After installing .net 4.5 custom forms authentications break

.net, asp.net, forms-authentication

Solution

I had to set the Current User myself in the Global.asax:

    private void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
    if (cookie != null)
    {
        FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
        HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(new FormsIdentity(ticket), new string[0]);
    }
}

Why this has changed in .Net 4.5 I don't know.

Problem

I have just upgraded to VS 2012 from VS2010, and are having some issues with FormsAuthentication. I have some old code which creates a custom auth cookie to store som information in it: ``` public static int SetAuthCookie<T>(this HttpResponse responseBase, string name, bool rememberMe, T userData) { JavaScriptSerializer serializer = new JavaScriptSerializer(); var cookie = FormsAuthentication.GetAuthCookie(name, rememberMe); var ticket = FormsAuthentication.Decrypt(cookie.Value); var newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, serializer.Serialize(userData), ticket.CookiePath); var encodedTicket = FormsAuthentication.Encrypt(newTicket); cookie.Value = encodedTicket; responseBase.Cookies.Add(cookie); return encodedTicket != null ? encodedTicket.Length : 0; } } ``` After upgrading to .Net 4.5 HttpContext.Current.Request.IsAuthenticated is always null. I have seen that there is a new way to authenticate in .Net 4.5, but I rather not use it, since I won't be able to upgrade the production enviroment from .Net 4.0. Is there a way to set the authentication when using a custom auth cookie?

Original source