Export (0) Print
Expand All

Roles.Providers Property

Gets a collection of the role providers for the ASP.NET application.

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

public static RoleProviderCollection Providers { get; }

Property Value

Type: System.Web.Security.RoleProviderCollection
A RoleProviderCollection that contains the role providers configured for the ASP.NET application.

ExceptionCondition
System.Configuration.Provider.ProviderException

Role management is not enabled.

The Providers property references all the role providers enabled for an application, including any providers added in the Web.config file. You can control which role providers are available for an application by using the providers element of the roleManager section in the Web.config file for your application.

The following example shows a roleManager section that removes any existing providers (such as those specified in the Machine.config file) and adds a SqlRoleProvider instance as the role provider for the application.

<configuration>
  <connectionStrings>
    <add name="SqlServices" connectionString="Data Source=MySqlServer;Integrated Security=SSPI;Initial Catalog=aspnetdb;" />
  </connectionStrings>

  <system.web>
    <roleManager defaultProvider="SqlProvider" 
      enabled="true"
      cacheRolesInCookie="true"
      cookieName=".ASPROLES"
      cookieTimeout="30"
      cookiePath="/"
      cookieRequireSSL="false"
      cookieSlidingExpiration="true"
      cookieProtection="Encrypted">

      <providers>
        <clear/>
        <add
          name="SqlProvider"
          type="System.Web.Security.SqlRoleProvider"
          connectionStringName="SqlServices"
          applicationName="MyApplication" />
         </providers>

    </roleManager>
  </system.web>
</configuration>

You can obtain a strongly typed reference to a provider from the Providers collection by indexing the role provider by name and casting it as the desired type.

You can obtain a reference to the default provider for an application using the Provider property.

The following code example lists the providers enabled for an application and their respective types.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Configuration.Provider" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>List Enabled Providers</title>
</head>
<body>

<%
foreach (RoleProvider p in Roles.Providers)
  Response.Write(p.Name + ", " + p.GetType() + "<br />");
%>

</body>
</html>

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft