I assume you are talking about doing this along with authentication. Here
is what I've done, and it works great. This was taken from an example on
MSDN.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT04.asp
'in global.asax
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As
EventArgs)
' Fires upon attempting to authenticate the user
'Extract the forms authentication cookie
Dim cookieName As String = FormsAuthentication.FormsCookieName
Dim authCookie As HttpCookie = Context.Request.Cookies(cookieName)
If authCookie Is Nothing Then
'There is no authentication cookie.
Return
End If
Dim authTicket As FormsAuthenticationTicket
Try
authTicket = FormsAuthentication.Decrypt(authCookie.Value)
Catch ex As Exception
' Log exception details (omitted for simplicity)
Return
End Try
If authTicket Is Nothing Then
' Cookie failed to decrypt.
Return
End If
Dim roles() As String = {"role1","role2","role3"}
' Create an Identity object
Dim id As FormsIdentity = New FormsIdentity(authTicket)
' This principal will flow throughout the request.
Dim principal As GenericPrincipal = New GenericPrincipal(id, roles)
' Attach the new principal object to the current HttpContext object
Context.User = principal
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim p As System.Security.Principal.IPrincipal =
HttpContext.Current.User
'username
Response.Write ("Your username " & p.Identity.Name)
If p.IsInRole("role1") Then
Response.Write("User is in role1")
Else
Response.Write("User is not in role1")
End If
End Sub