Share via


Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'System.Web.Security.RolePrincipal'.

Question

Friday, November 12, 2010 11:54 AM

I have a web site that is using windows authentication. 'Enable anonymous access' is unchecked. It works when I call it through Visual Studio but when I put the site on the server I get the following error:

Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'System.Web.Security.RolePrincipal'.

Here is the offending line:

            System.Security.Principal.GenericPrincipal genericPrincipal = (System.Security.Principal.GenericPrincipal)HttpContext.Current.User;

System.Security.Principal.GenericPrincipal genericPrincipal = (System.Security.Principal.GenericPrincipal)HttpContext.Current.User;

web.config

<authentication mode="Windows"/>

<identity impersonate="true"/>

Any help is appreciated.

All replies (3)

Thursday, November 18, 2010 3:16 AM âś…Answered

Hi,

According to your excpetion, but I cannot find any conversion between RolePrincipal and GenericPrincipal.

When using Windows Authentication, as it mentioned, I suggest to use 

System.Security.Principal.WindowsPrincipal winPrincipal = (System.Security.Principal.WindowsPrincipal)HttpContext.Current.User

rather than

System.Security.Principal.GenericPrincipal genericPrincipal = (System.Security.Principal.GenericPrincipal)HttpContext.Current.User

Sometimes this will cause cast exception.

BTW, do you enable RoleManager in web.config? if is, alos make sure this RoleManager provider is set correct. Or disable the RoleManager.

Hope this can help you.


Friday, November 12, 2010 11:23 PM

Can you pls explain what you are trying to do?

You can get the current authenticated user in two ways

System.Web.HttpContext.Current.User.Identity

or

System.Security.Principal.WindowsIdentity.GetCurrent();


Monday, November 15, 2010 9:03 AM

Bump.