today I've encountered with strange issue. My site was running on application pool under .NET 4 and today we noticed that it can't authenticate users under IE 10 (with other browsers everything is ok). Here's the exception that was thrown:
Server Error in '/' Application.
Unable to cast object of type 'System.Security.Principal.GenericIdentity' to type 'System.Web.Security.FormsIdentity'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Security.Principal.GenericIdentity' to type 'System.Web.Security.FormsIdentity'.
But after upgrading .NET 4 to version 4.5 error has gone away. The strange thing is that we didn't change the version of .NET on application pool, it is still .NET 4.
By the way I'm using custom principal and I'm attaching userData to AuthenticationTicket. Here's my code from the Global.asax:
protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
HttpCookie authCooke = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCooke != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCooke.Value);
if (authTicket != null)
{
var identity = new GenericIdentity(authTicket.Name, "Forms");
var principal = new CustomPrincipal(identity);
string userData = ((FormsIdentity)(Context.User.Identity)).Ticket.UserData;
var serializer = new JavaScriptSerializer();
principal.User = (User)serializer.Deserialize(userData, typeof(User));
Context.User = principal;
Thread.CurrentPrincipal = principal;
}
}
}
Could anybody explain to me what I am doing wrong and how updating version of .NET without changing it on application pool could affect the site?