SitePrinciple and SiteIdentity casting problems
I'm working on a project that I've adapted from this book, but I'm getting cast errors for my implementation of IIdentity and IPrincipal.
One such error is:
Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'AlfordDocs.WebModules.Accounts.Business.SitePrinc ipal'.
Which occurs when I do:
If CType(Context.User, SitePrincipal).HasPermission( _
CInt(ContentPermissions.CreatePages)) _
AndAlso TypeOf Context.User Is SitePrincipal Then
'bla bla bla
End If
Another such error is:
Unable to cast object of type 'System.Web.Security.FormsIdentity' to type 'AlfordDocs.WebModules.Accounts.Business.SiteIdent ity
Which occurs when running this line of code:
If Context.User.Identity.IsAuthenticated Then
Dim id As SiteIdentity = CType(Context.User.Identity, SiteIdentity)
UserName.Text = id.UserName
Else
Response.Redirect("/WebModules/Accounts/?Forward=" & Request.FilePath)
End If
However, I get no errors at all in my SiteHeader.ascx control, when I execute the following code:
UserGreetingListItem.InnerHtml = "Welcome"
If Context.User.Identity.IsAuthenticated Then
Dim id As SiteIdentity = CType(Context.User.Identity, SiteIdentity)
UserGreetingListItem.InnerHtml = UserGreetingListItem.InnerHtml _
& ", <strong>" & id.UserName & "</strong>!"
Else
UserGreetingListItem.InnerHtml = UserGreetingListItem.InnerHtml _
& ", <strong>guest</strong>!"
End If
This seems crazy. There is very little difference between the code fragments. I'd like to know why the first two throw cast errors but the last one doesn't, and where I should look to fix the problem.
Does anybody have any ideas please?
Many thanks
Ben
|