In the previous post I showed You how to create custom log in page for forms authentication. Now I’ll explain how to manage roles from the code side.

The main class which lets You manage roles from provider is System.Web.Security.SqlRoleProvider class.

To get the SqlRoleProvider object You have to use System.Web.Security.Roles class and pass the name of the provider You want to obtain:


SqlRoleProvider provider = Roles.Providers["ProviderName"] as SqlRoleProvider;

To get the names of all existing roles in the provider:


string[] roleNames = provider.GetAllRoles();

To add a new role:


provider.CreateRole("RoleName");

To delete role:


provider.DeleteRole("RoleName");

To add a user to the role:


provider.AddUsersToRoles(new string[] { "UserName" }, new string[] { "RoleName" });

To get a list of all users in the role:


string[] users = provider.GetUsersInRole("RoleName");

To remove users from the role:


provider.RemoveUsersFromRoles(new string[] { "UserName" }, new string[] { "RoleName" });