Microsoft KB Archive/306238

= How To Implement Role-Based Security with Forms-Based Authentication in Your ASP.NET Application by Using Visual Basic .NET =

Article ID: 306238

Article Last Modified on 10/29/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q306238



For a Microsoft Visual C# .NET version of this article, see 311495.

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



SUMMARY
This article describes how to implement role-based security in an ASP.NET application that implements forms-based authentication using Visual Basic .NET.

back to the top

Requirements
This article assumes that you have already implemented forms-based authentication on an ASP.NET application. For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

308157 How To Implement Forms-Based Authentication in Your ASP.NET Application by Using Visual Basic .NET

back to the top

Assign the Roles to the Authenticating User
Because 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 illustrated in the sample code to follow.

Copy the following code in your Global.asax file in your existing application to assign the roles to the authenticating user in the Application_AuthenticateRequest event handler: Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs) if (not(HttpContext.Current.User is Nothing)) then if HttpContext.Current.User.Identity.AuthenticationType = &quot;Forms&quot; then Dim id as System.Web.Security.FormsIdentity id = HttpContext.Current.User.Identity

Dim MyRoles(2) As String MyRoles(0) = &quot;Manager&quot; MyRoles(1) = &quot;Admin&quot; HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id,MyRoles) End if End if End sub back to the top

Check the User Roles and Implement the Program Logic in Your ASPX Pages
The following steps demonstrate how to implement and control the program logic based on the roles to which the authenticating user belongs.   Create a new .aspx page named Sample.aspx, and paste the following code: <%@ Page Language=&quot;VB&quot; %> <%@ Import Namespace=&quot;System.Web&quot; %>

Sub Page_Load(Src As Object, E As EventArgs) if User.IsInRole(&quot;Admin&quot;) then Response.Write (&quot;You are an Administrator&quot;) Else Response.Write (&quot;You do not have any role assigned&quot;) End if   End Sub

 Save Sample.aspx in your existing application. Browse to the page to test it.

back to the top

