Click here to Skip to main content
15,067,335 members
Articles / Web Development / ASP.NET
Posted 17 Sep 2002


819 bookmarked

Role-based Security with Forms Authentication

Rate me:
Please Sign up or sign in to vote.
4.91/5 (273 votes)
26 Jul 2009CPOL9 min read
Provides insight and tips on using role-based (groups) Forms Authentication in ASP.NET, which has only partial support for roles.


Forms Authentication in ASP.NET can be a powerful feature. With very little code and effort, you can have a simple authentication system that is platform-agnostic. If your needs are more complex, however, and require more efficient controls over assets, you need the flexibility of groups. Windows Authentication gives you this flexibility, but it is not compatible with anything but Internet Explorer since it uses NTLM, Microsoft's proprietary authentication system. Now you must choose how to manage your assets: provide multiple login pages / areas and force users to register for each, or assign groups to users and limit access to pages / areas to particular groups. Obviously, you must choose the latter.

Role-based security in Forms Authentication is one thing Microsoft left out in this round for .NET, but they didn't leave you high-and-dry. The mechanisms are there, they're just not intuitive to code. This tutorial will cover the basics of Forms Authentication, how to adapt it to make use of role-based security, and how to implement role-based security on your site with single sign-ons.

Updated: With ASP.NET 2.0, Microsoft introduced built-in support for role membership. If you're using ASP.NET 2.0 or newer it's recommended you read Managing Authorization using Roles on MSDN. You can use an abstract data provider or create your own. This article was written for ASP.NET 1.0 but will also work for 1.1.


This tutorial is all about role-based security with Forms Authentication, a detail that Microsoft left out of .NET for this round. This tutorial will use different techniques that are almost completely incompatible with the standard Forms Authentication, save the setup, which we'll cover shortly.

To follow along in this tutorial, you'll need to create a database, a web application, several secured directories, and a few ASP.NET Web Forms (pages).

Creating the Database

We will create a simple database containing a flat table for this tutorial. Using the <credentials/> section of the Web.config file is not an option because no mechanism for roles is supported. For the purposes of brevity, the table we create will be very simple. You're welcome to expand the database to make use of relations (what I would do and actual do use on several sites) for roles. The implementation does start to get a little messy depending on how you do it, and the details are left up to you. This is merely a tutorial about developing role-based security.

So, choose what database management system you want to use (DBMS). For this tutorial, we'll choose the Microsoft Data Engine (MSDE) available with Visual Studio .NET, Office XP Developer, and several other products. We'll add one database, say web, and then add one table, say users. To the users table, we'll add three fields: username, password, and roles. Set the username field to the primary key (since it'll be used for look-ups and needs to be unique), and optionally create an index on the username and password fields together. If you're using Table-creation SQL Scripts, your script might look something like this:


    username nvarchar(64) CONSTRAINT users_PK PRIMARY KEY,
    password nvarchar(128),
    roles nvarchar(64)

CREATE INDEX credentials ON users

Feel free to add some credentials to your database, picking a few roles you think are good group names for your site, such as "Administrator", "Manager", and "User". For this tutorial, put them in comma-delimited format in the "roles" field like the following, pipe-delimited (|) table:


Take note to make the roles case-sensitive. Now let's move on to creating our pages necessary for role-based Forms Authentication.

Creating the Login Pages

If you haven't already done so, create a new Web Application, or attach to an existing Web Application, such as your web server's document root, "/". For this tutorial, we'll assume the Web Application resides in "/", though the procedure for any Web Application is the same.

Before we create any pages or setup our Web.config file, you must understand one thing: the login.aspx (or whatever you call your login page) must be public. If it isn't, your users will not be able to log-in, and could be stuck in an infinite loop of redirects, though I've not tested this and don't care to. So, this tutorial will assume that login.aspx is in "/", while we have two secured sub-directories, users and administrators.

First, we must create a Forms Authentication login system that supports roles. Because Microsoft did not provide for this easily, we will have to take over the process of creating the authentication ticket ourselves! Don't worry, it's not as hard as it sounds. A few pieces of information are needed, and the cookie has to be stored under the right name - the name matching the configured name for Forms Authentication in your root Web.config file. If these names don't match, ASP.NET won't find the Authentication Ticket for the Web Application and will force a redirect to the login page. For simplicity, we will put the code directly into the ASP.NET Web Form, which is easier to code for DevHood and should look something like the following:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
// If you're using code-behind, make sure you change "private" to
// "protected" since the .aspx page inherits from the .aspx.cs
// file's class
private void btnLogin_Click(Object sender, EventArgs e)
    // Initialize FormsAuthentication, for what it's worth

    // Create our connection and command objects
    SqlConnection conn =
     new SqlConnection("Data Source=localhost;Initial Catalog=web;");
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT roles FROM web WHERE username=@username " +
     "AND password=@password";

    // Fill our parameters
    cmd.Parameters.Add("@username", SqlDbType.NVarChar, 64).Value =
    cmd.Parameters.Add("@password", SqlDbType.NVarChar, 128).Value =
        Password.Value, "md5"); // Or "sha1"

    // Execute the command
    SqlDataReader reader = cmd.ExecuteReader();
    if (reader.Read())
     // Create a new ticket used for authentication
     FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
        1, // Ticket version
        Username.Value, // Username associated with ticket
        DateTime.Now, // Date/time issued
        DateTime.Now.AddMinutes(30), // Date/time to expire
        true, // "true" for a persistent user cookie
        reader.GetString(0), // User-data, in this case the roles
        FormsAuthentication.FormsCookiePath);// Path cookie valid for

     // Encrypt the cookie using the machine key for secure transport
     string hash = FormsAuthentication.Encrypt(ticket);
     HttpCookie cookie = new HttpCookie(
        FormsAuthentication.FormsCookieName, // Name of auth cookie
        hash); // Hashed ticket

     // Set the cookie's expiration time to the tickets expiration time
     if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

     // Add the cookie to the list for outgoing response

     // Redirect to requested URL, or homepage if no previous page
     // requested
     string returnUrl = Request.QueryString["ReturnUrl"];
     if (returnUrl == null) returnUrl = "/";

     // Don't call FormsAuthentication.RedirectFromLoginPage since it
     // could
     // replace the authentication ticket (cookie) we just added
     // Never tell the user if just the username is password is incorrect.
     // That just gives them a place to start, once they've found one or
     // the other is correct!
     ErrorLabel = "Username / password incorrect. Please try again.";
     ErrorLabel.Visible = true;

    <p>Username: <input id="Username" runat="server"
type="text"/><br />
    Password: <input id="Password" runat="server" type="password"/><br
    <asp:Button id="btnLogin" runat="server" OnClick="btnLogin_Click"
    <asp:Label id="ErrorLabel" runat="Server" ForeColor="Red"

You'll notice above that we do one other thing with our passwords: we hash them. Hashing is a one-way algorithm that makes a unique array of characters. Even changing one letter from upper-case to lower-case in your password would generate a completely different hash. We'll store the passwords in the database as hashes, too, since this is safer. In a production environment, you'd also want to consider having a question and response challenge that a user could use to reset the password. Since a hash is one-way, you won't be able to retrieve the password. If a site is able to give your old password to you, I'd consider steering clear of them unless you were prompted for a client SSL certificate along the way for encrypting your passphrase and decrypting it for later use, though it should still be hashed.

Note: without using HTTP over SSL (HTTPS), your password will still be sent in plain-text across the network. Hashing the password on the server only keeps the stored password secured. For information about SSL and acquiring a site or domain certificate, see or

If you don't want to store hashed passwords in the database, change the line that reads FormsAuthentication.HAshPasswordForStoringInConfigFile(Password.Value, "md5") to just Password.Value.

Next, we'll need to modify the Global.asax file. If your Web Application doesn't have one already, right-click on the Web Application, select "Add->Add New Item...->Global Application Class". In either the Global.asax or Global.asax.cs (or Global.asax.vb, if you're using VB.NET), find the event handler called Application_AuthenticateRequest. Make sure it imports / uses the System.Security.Principal namespace and modify it like so:

protected void Application_AuthenticateRequest(Object sender,
EventArgs e)
  if (HttpContext.Current.User != null)
    if (HttpContext.Current.User.Identity.IsAuthenticated)
     if (HttpContext.Current.User.Identity is FormsIdentity)
        FormsIdentity id =
        FormsAuthenticationTicket ticket = id.Ticket;

        // Get the stored user-data, in this case, our roles
        string userData = ticket.UserData;
        string[] roles = userData.Split(',');
        HttpContext.Current.User = new GenericPrincipal(id, roles);

What's happening above is that since our principal (credentials - which are your username and roles) is not stored plainly as part of our cookie (nor should it, since a user could modify their list of role-memberships), it needs to be generated for each request. The FormsAuthenticationTicket is actually encrypted as part of a cookie using your machine key (usually configured in machine.config) and the FormsAuthentication module decrypts the tick as part of the user's identity. If you search long and hard enough on Microsoft MSDN web site, you'll find this documentation buried. We use the UserData to obtain the list of roles and generate a new principal. Once the principal is created, we add it to the current context for the user, which the receiving page can use to retrieve credentials and role-memberships.

Securing Directories with Role-based Forms Authentication

To make the role-based authentication work for Forms Authentication, make sure you have a Web.config file in your Web Application root. For the authentication setup, this particular Web.config file must be in your Web Application's document root. You can override the <authorization/> in Web.config files for sub-directories.

To begin, make sure your Web.config file has at least the following:

        <authentication    mode="Forms">
            <forms name="MYWEBAPP.ASPXAUTH"
            <allow users="*"/>

The FormsAuthentication name (MYWEBAPP.ASPXAUTH) above it arbitrary, although the name there and the name in the HttpCookie we created to hold the hashed FormsAuthenticationTicket must match, for even though we are overriding the ticket creation, ASP.NET still handles the authorization automatically from the Web.config file.

To control authorization (access by a particular user or group), we can either 1) add some more elements to the Web.config file from above, or 2) create a separate Web.config file in the directory to be secure. While, I prefer the second, I will show the first method:

        <authentication mode="Forms">
            <forms name="MYWEBAPP.ASPXAUTH"
            <allow users="*"/>
    <location path="administrators">
                <!-- Order and case are important below -->
                <allow roles="Administrator"/>
                <deny users="*"/>
    <location path="users">
                <!-- Order and case are important below -->
                <allow roles="User"/>
                <deny users="*"/>

The Web Application always creates relative paths from the paths entered here (even for login.aspx), using it's root directory as the starting point. To avoid confusion with that condition and to make directories more modular (being able to move them around without changing a bunch of files), I choose to put a separate Web.config file in each secure sub-directory, which is simply the <authorization/> section like so:

            <!-- Order and case are important below -->
            <allow roles="Administrator"/>
            <deny users="*"/>

Notice, too, that the role(s) is/are case-sensitive. If you want to allow or deny access to more than one role, delimit them by commas.

That's it! Your site is setup for role-based security. If you use code-behind, compile your application first. Then try to access a secure directory, such as /administrators, and you'll get redirected to the login page. If login was successful, you're in, unless your role prohibits it, such as the /administrators area. This is hard for the login.aspx page to determine, so I'd recommend a Session variable to store the login attempts and after so many times, return an explicit "Denied" statement. There is another way, however, which is discussed below.

Conditionally Showing Controls with Role-based Forms Authentication

Sometimes it's better to show / hide content based on roles when you don't want to duplicate a bunch of pages for various roles (user groups). Such examples would be a portal site, where free- and membership-based accounts exist and membership-based accounts can access premium content. Another example would be a news page that would display an "Add" button for adding news links if the current user is in the "Administrator" role. This section describes how write for such scenarios.

The IPrincipal interface, which the GenericPrincipal class we used above implements, has a method called IsInRole(), which takes a string designating the role to check for. So, if we only want to display content if the currently logged-on user is in the "Administrator" role, our page would look something like this:

  <script runat="server">
  protected void Page_Load(Object sender, EventArgs e)
   if (User.IsInRole("Administrator"))
    AdminLink.Visible = true;
  <p>Welcome, anonymous user, to our web site.</p>
  <asp:HyperLink id="AdminLink" runat="server"
   Text="Administrators, click here." NavigateUrl="administrators/"/>

Now the link to the Administrators area of the web site will only show up if the current user is logged in and is in the "Administrator" role. If this is a public page, you should provide a link to the login page, optionally setting the QueryString variable called ReturnUrl to the path on the server you want the user to return to upon successful authentication.


This tutorial was created to help you understand the important of role-based security, as well as implement role-based security on your web site with ASP.NET. It's not a hard mechanism to implement, but it does require some know-how of what principals are, how credentials are authenticated, and how users / roles are authorized. I hope you have found this tutorial helpful and interesting, and that it leads you to implement role-based Forms Authentication on your current or upcoming site!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Heath Stewart
Software Developer Microsoft
United States United States
Heath Stewart is a happily married software engineer originally from the Midwest and a graduate of Iowa State University. Heath start programming early in life and enjoys continuous research and development in new languages, frameworks, and platforms. Fluent in many different programming languages, he has developed many large-scale software solutions for companies in different areas, such as Internet filtering, intrusion detection systems, production management systems, and web applications for various purposes. He also enjoys photography.

Currently, Heath is a Program Manager in the Visual Studio Professional Deployment Experience (VSPro DEX) team at Microsoft. Previous to his employment, he was a Microsoft MVP for Visual C#.

He is also a CodeProject protector and is happy to help the development community.

Comments and Discussions

Questionarticle Pin
Kris321130-Jun-20 1:34
professionalKris321130-Jun-20 1:34 
QuestionThanks, performance improvement? Pin
Member 1305700031-Oct-18 6:42
MemberMember 1305700031-Oct-18 6:42 
QuestionI want video for this role authentication Pin
Member 133527878-Aug-17 20:42
MemberMember 133527878-Aug-17 20:42 
GeneralThank you Pin
bahrammmmmm13-Dec-16 0:23
Memberbahrammmmmm13-Dec-16 0:23 
PraiseVery helpful !!! Pin
Member 1032504323-Feb-16 23:50
MemberMember 1032504323-Feb-16 23:50 
GeneralMy vote of 5 Pin
Member 1188441012-Feb-16 20:33
MemberMember 1188441012-Feb-16 20:33 
QuestionHttpContext.Current.User is always NULL Pin
JavaDummie22-Dec-15 20:45
MemberJavaDummie22-Dec-15 20:45 
AnswerMessage Closed Pin
26-Jan-16 22:37
MemberMember 1229005126-Jan-16 22:37 
QuestionNot Redirecting to Admin or User page Pin
Avi Roy8-Sep-15 5:56
MemberAvi Roy8-Sep-15 5:56 
Questioni am getting 403.14 error olz help me as early as possible Pin
krishna24626-Dec-14 18:53
Memberkrishna24626-Dec-14 18:53 
GeneralGood Pin
majid99h7-Aug-14 0:58
Membermajid99h7-Aug-14 0:58 
QuestionQuestion about roled base Security Pin
Member 1088999020-Jun-14 3:47
MemberMember 1088999020-Jun-14 3:47 
QuestionUnable to cast an object of type 'System.Int32' to type 'System.String' Pin
Member 1088999020-Jun-14 1:45
MemberMember 1088999020-Jun-14 1:45 
Questioncorrection on query Pin
Member 418365314-Nov-13 8:26
MemberMember 418365314-Nov-13 8:26 
QuestionImplementation in Vb.Net and SQL Server Pin
Member 100250052-May-13 8:59
MemberMember 100250052-May-13 8:59 
GeneralMy vote of 5 Pin
chandrasekharl8-Nov-12 21:31
Memberchandrasekharl8-Nov-12 21:31 
QuestionRole-based Security with Forms Authentication Pin
Member 931980528-Oct-12 4:50
MemberMember 931980528-Oct-12 4:50 
GeneralDoesnt Seem to Work for Images Pin
manofatlantic6-Aug-12 10:38
Membermanofatlantic6-Aug-12 10:38 
QuestionQuestion regarding form based authentication Pin
vishalpatwardhan12-Jul-12 4:29
Membervishalpatwardhan12-Jul-12 4:29 
GeneralMy vote of 4 Pin
vicvis28-Jun-12 3:10
Membervicvis28-Jun-12 3:10 
Questionadministrators page is still not visible Pin
vicvis28-Jun-12 3:09
Membervicvis28-Jun-12 3:09 
Generalquite good article for understanding of form authenticaion. Pin
srichavan2-Jun-12 18:06
Membersrichavan2-Jun-12 18:06 
Generalquite good article for understanding of form authenticaion Pin
srichavan2-Jun-12 18:05
Membersrichavan2-Jun-12 18:05 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey28-Feb-12 18:28
professionalManoj Kumar Choubey28-Feb-12 18:28 
GeneralMy vote of 5 Pin
ishan58827-Nov-11 22:48
Memberishan58827-Nov-11 22:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.