Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

I think I read all of the information in the whole internet regarding the CustomPrincipal class. I have every tutotorial and reference that I could find about inplemending this, and all show the same code. A lot of people like myself are having problems when trying to cast the HttpContext.User to the CustomPrincipal Class. Maybe someone here has an answer.

 

 

Default.aspx

<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Security.Principal" %>
<%@ Assembly Src="CustomPrincipal.vb" %>

Sub Page_Load
If Not IsPostBack Then

End If

'Dim cp As CustomPrincipal = CType(HttpContext.Current.User, CustomPrincipal) ' gives invalid cast error

lblMessage.Text = "User: " & HttpContext.Current.User.Identity.Name ' should be cp.Identity.Name

End Sub

 

Global.asax

 

 
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Security.Principal" %>
<%@ Assembly Src="CustomPrincipal.vb" %>
<script language="VB" runat="server">
Sub Application_AuthenticationRequest(sender As Object, e As EventArgs)
' 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 = Nothing

Try
authTicket = FormsAuthentication.Decrypt(authCookie.Value)
Catch ex As Exception
'Return
End Try

If (authTicket Is Nothing) Then
' Cookie failed to decrypt
'Return
End If

' When the ticket was created, the UserData property was assigned a 
' comma delimited string of role names
Dim roles As String() = authTicket.UserData.Split(New Char() {","})

' Create an Identity object
Dim id As FormsIdentity = new FormsIdentity(authTicket)

' This principal will flow throughout the request
Dim principal As CustomPrincipal = new CustomPrincipal(id, roles)

' Attach the new principal object to the current HttpContext object
Context.User = principal

End Sub
</script>

 

 

CustomPrincipal.aspx

 

 
Imports System
Imports System.Security.Principal

Public Class CustomPrincipal 
Implements IPrincipal
Private _identity As IIdentity
Private _roles() As String
Public Sub New (ByVal identity As IIdentity, ByVal roles As String())
_identity = identity
_roles = New String(roles.Length) {}
roles.CopyTo(_roles, 0)
Array.Sort(_roles)
End Sub

' IPrincipal Implementation
Public Overridable Function IsInRole(ByVal role As String) As Boolean Implements IPrincipal.IsInRole
If Array.BinarySearch(_roles, role) >=0 Then
Return True
Else
Return False
End If
End Function

Public ReadOnly Property Identity() As IIdentity Implements IPrincipal.Identity
Get 
Return _identity
End Get
End Property

' Checks whether a principal is in all of the specified set of roles
Public Function IsInAllRoles(ByVal roles As String()) As Boolean
Dim searchrole As String
For Each searchrole In roles
If Array.BinarySearch(_roles,searchrole) < 0 Then
Return False
End If
Next
Return True
End Function

' Checks whether a principal is in any of the specified set of roles
Public Function IsInAnyRoles(ByVal roles As String()) As Boolean
Dim searchrole As String
For Each searchrole In roles
If Array.BinarySearch(_roles,searchrole) > 0 Then
Return True
End If
Next
Return False
End Function
End Class

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...