SqlRoleProvider Class

Manages storage of role membership information for an ASP.NET application in a SQL Server database.

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

public class SqlRoleProvider : RoleProvider
public class SqlRoleProvider extends RoleProvider
public class SqlRoleProvider extends RoleProvider
Not applicable.

This class is used by the Roles and RolePrincipal classes to provide role-management services for an ASP.NET application using a SQL Server database. You can use role management to specify different levels of authorization for your application.

To use the SqlRoleProvider class, you must first create the SQL Server database used by the SqlRoleProvider. To create the database used by the SqlRoleProvider class, run the aspnet_regsql.exe executable found in the C:\WINDOWS\Microsoft.NET\Framework\ versionNumber folder and specify the -Ar option (for example, aspnet_regsql.exe -Ar). The database created is called Aspnetdb. Alternatively, run aspnet_regsql.exe to pull up the GUI configuration mode and choose to configure all ASP.NET features.

If the role provider is configured with a connection string that uses integrated security, the process account of the ASP.NET application must have rights to connect to the SQL Server database.

The Machine.config file is configured with a SqlRoleProvider instance named AspNetSqlProvider that connects to the SQL Server on the local machine. You can use this instance of the provider, or specify your own in the Web.config file for your ASP.NET application. To use the AspNetSqlProvider instance, specify AspNetSqlProvider as the defaultProvider in your roleManager configuration.

You can configure the SqlRoleProvider to use the same database and user information as the SqlMembershipProvider in order to use a single database for authentication and authorization information. To use the same database for membership and role information, run the aspnet_regsql.exe executable and install the membership feature. Then, specify the same connection string in your configuration for both your SqlRoleProvider and SqlMembershipProvider instances. Also ensure that both provider instances are configured with the same ApplicationName.

The following example shows the Web.config file for an ASP.NET application configured to use a SqlRoleProvider object and the SqlMembershipProvider. The authorization element is configured to only allow access to authenticated users in the Administrators role.

    <add name="SqlServices" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=aspnetdb;" />
    <authentication mode="Forms" >
      <forms loginUrl="logincs.aspx"
      name=".ASPXFORMSAUTH" />
      <deny users="?" />
         <allow roles="Administrators" />
         <deny users="*" />
    <membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">
          applicationName="SampleApplication" />
    <roleManager defaultProvider="SqlProvider" 
      cookieProtection="All" >
          applicationName="SampleApplication" />

  • AspNetHostingPermission  for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
  • AspNetHostingPermission  for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0