Export (0) Print
Expand All

Roles.Provider Property

Gets the default role provider for the application.

Namespace:  System.Web.Security
Assembly:  System.Web (in System.Web.dll)

Public Shared ReadOnly Property Provider As RoleProvider
Dim value As RoleProvider 

value = Roles.Provider

Property Value

Type: System.Web.Security.RoleProvider
The default role provider for the application, which is exposed as a class that inherits the RoleProvider abstract class.


Role management is not enabled.

The Provider property enables you to directly reference the default role provider for an application. This is commonly used to access custom members of the role provider that are not part of the RoleProvider abstract class.

For example, the WindowsTokenRoleProvider class includes an overload of the IsUserInRole method that enables you to determine whether a user is in a common Windows role by using a WindowsBuiltInRole enumeration value. A reference to the WindowsTokenRoleProvider class for an application can be obtained by using the Provider property and can be cast as a WindowsTokenRoleProvider in order to refer to the IsUserInRole overload.

If multiple role providers are configured for an application, you can access different role providers using the Providers collection.

The following code example casts the default role provider as a WindowsTokenRoleProvider and checks whether the currently logged-on user is in the Administrators role before allowing the user to view roles settings for the application. For an example of a Web.config file that enables role management, see WindowsTokenRoleProvider.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<script runat="server">

Dim rolesArray() As String 

Public Sub Page_Load()
  Msg.Text = "" 

  Dim provider As WindowsTokenRoleProvider = CType(Roles.Provider, WindowsTokenRoleProvider)

  If Not provider.IsUserInRole(User.Identity.Name, _
                               System.Security.Principal.WindowsBuiltInRole.Administrator) Then
    Msg.Text = "You are not authorized to view user roles." 
  End If 

  ' Bind roles to GridView. 

    rolesArray = Roles.GetRolesForUser(User.Identity.Name)
  Catch e As HttpException
    Msg.Text = "There is no current logged on user. Role membership cannot be verified." 
  End Try

  UserRolesGrid.DataSource = rolesArray

  UserRolesGrid.Columns(0).HeaderText = "Roles for " & User.Identity.Name
End Sub

<html xmlns="http://www.w3.org/1999/xhtml" >
<title>Sample: View User Roles</title>

<form runat="server" id="PageForm">

  <h3>View User Roles</h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />

  <table border="0" cellspacing="4">
      <td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid" 
                                     AutoGenerateColumns="false" Gridlines="None" 
                                     CellSpacing="0" >
                         <HeaderStyle BackColor="navy" ForeColor="white" />
                           <asp:TemplateField HeaderText="Roles" >
                               <%# Container.DataItem.ToString() %>



Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

© 2015 Microsoft