In this article we will tell you how to do role based authorization.
This article is very useful for security purposes.
Suppose we have the three folders in our web site AdminUser, ClientUser and PartnerUser. In that folder there are some aspx pages. I want that the user with admin rights to be able to see the the aspx pages in the AdminUser folder. If the admin user attempts to open the pages that are in the ClientUser folder and the PartnerUser folder then the website automatically redirects the admin user to the login page. Similarly, the client and partner users can only access the pages in which they have rights.
To do role based security please use the following procedure.
1. User Credential store in web.config
For Admin User
- <!--Path: folder path -->
- <location path="AdminUser">
- <system.web>
- <authorization>
- <!-- Allow user who have Admin role can access the AdminUser folder aspx pages -->
- <allow roles="Admin"/>
- <!-- Other user can not access AdminUser folder aspx pages -->
- <deny users="*"/>
- </authorization>
- </system.web>
- </location>
For Client User
- <!--Path: folder path -->
- <location path="ClientUser">
- <system.web>
- <authorization>
- <!-- Allow user who have Client role can access the ClientUser folder aspx pages -->
- <allow roles="Client"/>
- <!-- Other user can not access ClientUser folder aspx pages -->
- <deny users="*"/>
- </authorization>
- </system.web>
- </location>
For Partner User
- <!--Path: folder path -->
- <location path="PartnerUser">
- <system.web>
- <authorization>
- <!-- Allow user who have Partner role can access the PartenerUser folder aspx pages -->
- <allow roles="Partner"/>
- <!-- Other user can not access Partner folder aspx pages -->
- <deny users="*"/>
- </authorization>
- </system.web>
- </location>
2. In Global.asax in the Application_AuthenticateRequest event create the security principal for the user role
- // Check that the request has been authenticated
- if (Request.IsAuthenticated)
- {
- // Get the role from the ticket
- string[] role = new string[1];
- role[0] = ((FormsIdentity)Context.User.Identity).Ticket.UserData;
- // Create a new GenericPrincipal with the role information
- System.Security.Principal.GenericPrincipal newPrincipal = new System.Security.Principal.GenericPrincipal(Context.User.Identity, role);
- // Add the principal to the security context, which replaces the current GenericPrincipal
- Context.User = newPrincipal;
- }
- FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, UserID.ToString(),
- DateTime.Now, DateTime.Now.AddMinutes(180), true, UserRole);
- // User data string, in our case, to hold the role
- string encryptedTicket = FormsAuthentication.Encrypt(ticket);
- HttpCookie authenticationCookie
- = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); Response.Cookies.Add(authenticationCookie);

Attiq JaffarPosted Oct 5, 2019, 3:30 PM
Thx for saving my day
Milburn GomesPosted Aug 16, 2018, 10:29 AM
Great tutorial! Exactly what I needed. Thanks a lot!
Purush SeoPosted Jul 8, 2015, 4:16 AM
Hello exactly am using your code initially it worked once executed after that it is not working with your code when i was changing location path as like ex: "~/PartnerUser" it is working but access all pages. could you please give me solution for this.