Subject: SitePrinciple and SiteIdentity casting problems
Posted By: BenCh Post Date: 8/15/2006 6:35:35 PM
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.SitePrincipal'.

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.SiteIdentity

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

Reply By: englere Reply Date: 8/15/2006 11:35:10 PM
You likely missed some of the obvious differences, like the folders and whether you have any old assemblies lying around. It's easy to see this kind of issue on systems that haven't had much housecleaning. And it's hard to research because you have a lot of bin folders.

The 2005 model is cleaner, where any custom security code can go in the app_code folder. But you often don't need custom security code in 2005 with simple sites.

Eric

Reply By: BenCh Reply Date: 8/16/2006 5:48:45 AM
Hi Englere,

Thanks for your response, but I'm not sure I understand it. Are you saying that it's not a problem with my code at all, but an issue with different versions of ASP .NET? I really wasn't expecting that at all, but if you think that's the most likely cause, could you please tell me more specifically what "differences" I'm looking for, and what the differences are between, as I'm not sure I follow your train of thought.

Many thanks
Ben

Reply By: englere Reply Date: 8/21/2006 10:47:28 AM
You need to understand how .net loads assemblies because you're probably not loading the assemlies you think you're loading. Which ones you think it's loading?

You need to get real familiar with your directories and what's in them. Make sure that anything old has been removed. I advise NOT having any of these assemblies in the GAC because that would just complicate updates and not really help you.

This type of issue is almost always caused by stale files lying around. Especially since you've done some custom modifications - are you sure that the original assemblies are gone? It's time to become a Windows Explorer power guru!

Eric



Go to topic 13482

Return to index page 198
Return to index page 197
Return to index page 196
Return to index page 195
Return to index page 194
Return to index page 193
Return to index page 192
Return to index page 191
Return to index page 190
Return to index page 189