This is a discussion on How to Implementing Role Based Security in ASP.NET within the C# Programming forums, part of the Software Development category; Implementing Role Based Security in ASP.NET Here is thread for how to implementing Role Based Security in ASP.NET....
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Implementing Role Based Security in ASP.NET Here is thread for how to implementing Role Based Security in ASP.NET.
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| Sponsored Links |
| |||
| ASP.NET allows three main ways to authenticating the user of the application. They are - Windows Authentication, Forms Based Authentication and Passport Authentication. Out of these three Windows and Forms authentications are most commonly used for intranet and internet applications respectively. Authentication involves validating that the user is what he claims to be. In many applications this is not just enough. You also need to grant access rights to the user based on his category. This process is referred to as authorization. The category I just mentioned is nothing but the role of the user. In this article we will see how to use Windows as well as Custom roles to authorize users of your application.
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Identities Identities are nothing but the users of your application and allow you to obtain information about that user. The classes (GenericIdentity and WindowsIdentity) and interfaces (IIdentity) required for working with Identities reside in the System.Security.Principal Namespace.
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Roles A role is nothing but a set of access rights that is assigned to the user. One user may have one or more roles. You must be familiar with Windows roles such as Administrator and Guest.
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Principals A Principal is combination of the identity and role(s) of the user. The classes and interfaces related to Principals (GenericPrincipal, WindowsPrincipal and IPrincipal) can be found in System.Security.Principal Namespace.
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| When to authorize a user? Note that you should authorize a user only after authenticating. The Request.IsAuthenticated property tells you whether the user is authenticated or not. You should check this in Application_AuthenticateRequest event handler in Global.asax. This event is fired for each request at the time of authenticating the user. If the user is already authenticated by windows or forms authentications then you follow above steps .
__________________ cheers Aman |
| |||
| Authentication in ASP.NET ASP.NET provides three ways to authenticate your users. They are: - Windows - Forms - Passport Out of these three the first two are commonly used in ASP.NET applications.
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! Last edited by S.Vinothkumar : 03-31-2008 at 09:40 PM. |
| |||
| Role based security and Windows Authentication When you use Windows authentication to authenticate a user, you also have roles for that user based on its Windows group. For example, a user User1 might belong to group Administrators and the same role can be used in ASP.NET applications. You can check whether a user belongs to a particular role or not you need to write something like this: Code: if(User.IsInRole("BUILTIN\Administrators")
{
//display all options
}
else
{
//display limited options
}
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Role based security and Forms authentication If you are authenticating users via Forms authentication then you need to take care of some extra steps. These steps are: - Create a user identity - Create an array of roles - Create a principal based on user identity and list of roles - Attach the principal to the current authenticated user
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Create a user identity User identity is represented by a class that implements IIdentity interface. .NET comes with a class GenericIdentity that is a simple implementation of IIdentity interface. Here, we will create a user identity using GenericIdentity class. Code: GenericIdentity gi=new GenericIdentity(User.Identity)
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Hi, Create an array of roles Next, we need to create an string array containing roles to which the user belongs. string[] roles={"clerk","manager"};
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Create a principal based on user identity and list of roles We will now create a principal based on the identity and role information that we have. .NET provides a class called GenericPrincipal that represents a simple implementation of IPrincipal. Code: GenericPrincipal gp=new GenericPrincipal(gi,roles);
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Attach the principal to the current authenticated user Finally, we need to attach the principal we just created to the ASP.NET application. The way you do this is as follows: Code: Context.User = gp;
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Authorizing the user Once you have done this, authorizing a user is same as in Windows authentication. Code: if(User.IsInRole("manager")
{
//display all options
}
else
{
//display limited options
}
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Implementing soap using PHP | Falcon | PHP Programming | 58 | 05-02-2008 02:54 AM |
| Product-based Company and Projects-based Company : | vigneshgets | Software Testing | 0 | 01-15-2008 10:03 PM |
| # What is the role of the DataReader class in ADO.NET connections? | anbuchezhians | VB.NET Programming | 1 | 07-27-2007 01:44 AM |
| What's the role of documentation in QA? | devarajan.v | Software Testing | 1 | 07-17-2007 07:16 AM |
| Implementing Crystal Report in Web Application | oxygen | C# Programming | 0 | 07-15-2007 10:40 PM |