After installing .net 4.5 user authentication forms break

I just upgraded to VS 2012 from VS2010 and have some issues with FormsAuthentication.

I have an old code that creates a custom 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 saw that there is a new authentication method in .Net 4.5, but I probably will not use it since I will not be able to update the production environment from .Net 4.0. Is there a way to set authentication when using a personalized cookie?

+4
source share
3 answers

I needed to set the current user myself in 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 do not know.

+4
source

It looks like you are facing one of these problems:

http://social.microsoft.com/Forums/en-US/Offtopic/thread/1791c5e3-4087-4e92-a460-51c5c4221f49

or a simpler configuration for it:

http://forums.asp.net/t/1835788.aspx/1

Or a broken web.config, like here:

http://forums.asp.net/t/1852961.aspx/1

+1
source

The machineKey handler is handled differently in .NET 4.5. You can add CompatMode = "Framework20SP1" to your configuration:

(The above is standard for 2.0 / 4.0 applications, but not the default value for 4.5 applications, so it should be explicitly stated in app 4.5.)

Link: http://forums.asp.net/t/1835788.aspx/1

+1
source

All Articles