Unable to cast object of type 'System.Security.Principal.GenericIdentity' to type 'System.Web.Security.FormsIdentity'

asp.net, c#, form-authentication

Solution

Apparently the principal object associated with current request was `GenericIdentity` rather than `FormsIdentity`. Casting between these two is not possible.

You should carefully inspect your application's stack with respect to identity management modules and all other possible places in your code where the identity is set for current request. If you are able to identify the culprit that sets the `GenericIdentity` - you are done, you could rewrite/redesign this particular spot.

My guess is that this problem occurs when the user is not authenticated. The runtime creates a `GenericIdentity` for current request and sets the `IsAuthenticated` to `false`. I'd rewrite the code to:

 if ( HttpContext.Current.User != null && HttpContext.Current.User.Identity is FormsIdentity )
 {
    // your code follows
 }
 else
 {
    // the user is not yet authenticated and 
    // there is no Forms Identity for current request
 }

Problem

I have get this error during the site log in.How can i resolve this problem.

Original source