This Article Was Previously Published Under Q311495
For a Microsoft Visual Basic .NET VERSION OF This Article, See
306238.
This Article Refers To The Following Microsoft .NET Framework Class Library Namespaces:
System.web.security system.security.principal
In this task
Summary
Requirements Assign The Roles To The Authenticating User Check The User Roles and Implement The Program Logic in Your ASPX Pages References
Summarythis article describes how to import role-based security in an asp.net application today implements forms-based Authentication Using Visual C # .NET
Back to the top
RequirementSthis Article Assumes That You Have Already Implement Forms-Based Authentication on an ASP.NET Application.
301240 How to Implement Forms-Based Authentication In Your ASP.NET Application Using C # .NET
Back to the top
Assign the Roles to the Authenticating UserBecause forms users usually are not Microsoft Windows users, they do not have any roles associated with them by default. Thus, you must attach the roles of the authenticating user to that user's identity so that you can implement the role -Based Security Inside your code.
Use the sample code in this section to implement role-based security in your application. This sample code assigns pre-specified roles to the authenticating user. Depending how you store your user data, you can implement your own method to retrieve the roles for that Authenticated User and attach those Roles to the Authenticating User's Identity, Which is illustrate in the sample code to follow.
Copy The Follow File in Your Existing Application To Assign The Roles To The Authenticating User In The Austrion
Application_AuthenTicateRequest Event Handler:
Public void Application_AuthenticateRerequest (Object Src, Eventargs E) {
IF (! (httpcontext.current.user == null))
{
IF (httpContext.current.user.Identity.AuthenticationType == "Forms")
{
System.web.security.formsidentity ID;
ID = (System.Web.Security.FormSidentity) httpContext.current.user.Identity;
String [] myroles = new string [2];
MYROLES [0] = "manager";
MYROLES [1] = "admin";
HttpContext.current.user = new system.security.principal.GenericPrincipal (ID, myroles);
}
}
}
Back to the top
Check the User Roles and Implement the Program Logic in Your .ASPX PagesThe following steps demonstrate how to implement and control the program logic based on the roles to which the authenticating user belongs.
Create a .aspx new page named sample.aspx, and paste the folload code: <% @ page language = "c #"%>
<% @ Import namespace = "system.web"%>
Public void Page_load () {
User.Isinrole ("admin")) {
Response.write ("you are an administrator);
Else {
Response.write ("You Do Not Have Any Role Assigned");
}
script>
Save Sample.aspx in your existing application. Browse to the page to test it.
Back to the top
Referencesfor Additional Information About ASP.NET Security Features, Click The Following Article Number To View The Article In The Microsoft Knowledge Base:
306590 Info: ASP.NET Security Overview for More Information About Role-Based Security, Refer to the Following .NET Framework Software Development Kit (SDK) Documentation:
Role-Based Securityhttp: //msdn.microsoft.com/library/default.asp url = / library / en-us / cpguide / html / cpconrole-basedsecurity.aspThe documentation and source code at the following IBuySpy Developer Solutions Web site also? contains information about role-based security: IBuySpy Developer Solutionshttp: //www.ibuyspy.comMicrosoft provides third-party contact information to help you find technical support This contact information may change without notice Microsoft does not guarantee the accuracy of this third-.. Party Contact Information.
Back to the top
The Information in this Article Applies TO:
Microsoft ASP.NET (Included with the .NET Framework) 1.0 Microsoft Visual C # .NET (2002) Microsoft ASP.NET (Included with the .NET Framework 1.1) Microsoft Visual C # .NET (2003)
Last Reviewed: 6/29/2004 (3.1) Keywords: KbhowTomaster Kbsecurity KB311495 KBAUDDEVEVELOPER