Wednesday 2 April 2014

Windows Authentication not setting User.IsAuthenticated in .NET 4.0

Problem

You’ve just upgraded your web project to .NET Framework 4.0 and all of a sudden your code that checked HttpContext.Current.User.Identity.Name returns a blank string. User.Identity.IsAuthenticated also returns false but strangely Request.ServerVariables["LOGON_USER"] gives the user name.

Solution

In your web.config add the following two keys to your appSettings:

<configuration>
<appSettings>
<add key="autoFormsAuthentication" value="false" />
<add key="enableSimpleMembership" value="false" />
</appSettings>
</configuration>



To me Microsoft have got lazy in their configuration stuff … why put this in the appSettings and not in the authentication section?



Digging Deeper



It looks like this problem may have been introduced in ASP.NET 3 but that in my case changing the .Net version up to 4 forced an upgrade to ASP.NET 3+ perhaps.



Technorati Tags: ,