ASP.NET MVC 3 Intranet Template with "This resource cannot be found" and User.Identity.Name is blank Error
ASP.NET MVC 3 Tools refresh recently was released and it includes a new Intranet Visual Studio project template. This template is basically an ASP.NET MVC 3 template with Windows Authentication enabled (the Internet Application template uses forms based authentication).
I was recently playing with this template and it worked fine in development and even using the IIS 7.x Express Web Server. However, when I deployed to a real Windows Server 2008 R2 application I saw this "The resource cannot be found" error:
So what is going on? Notice (from the screenshot above) that we are being redirected to .../Account/Login. This is a clue that the login/authentication is failing. The controller for login is throwing the error.
If you explicitly actually type in /Home/About you will actually get a rendered page below. Notice in the top right corner all you see is an exclamation point. The @User.Identity.Name code in the _Layout.cshtml is blank/NULL and causing the error (shown below that is why you only see "Welcome !").
Why is Windows Authentication not working? It looks to me that this is a bug in the ASP.NET MVC 3 Intranet template....I stumbled upon this by chance, by going through the "known issues" section in the ASP.NET MVC 3 release note
s. It mentions that we should add this line (<add key="enableSimpleMembership" value="false" />) to the web.config if we are upgrading from an ASP.NET MVC 2 application. This has NOTHING to do BTW with Windows Authentication and I tried adding that key in web.config by chance and it properly authenticated me.
After adding the key (and restarting the application pool), ASP.NET MVC 3 properly is able to grab the identity of the user via Windows Authentication. Basically the appsettings section of the web.config should look like this now:
<add key="webpages:Version" value="126.96.36.199" />
<add key="ClientValidationEnabled" value="true" />
<add key="enableSimpleMembership" value="false" />
If you receive a "This Resource Cannot be Found" error or the User.Identity.Name is blank or IsAuthenticated is false for an ASP.NET MVC application ensure:
- Windows Authentication is turned on in web.config (IIS 6 and 7)
- You are using the Integrated pipeline mode in the application pool (IIS 6 and 7)
- All the wildcard mappings and ISAPI settings are properly set up (IIS 6)
- Add the key: <add key="enableSimpleMembership" value="false" /> to the appSettings section
- If you have Kerebos/NTLM etc authentication, make sure it is configured correctly in IIS
I hope this article helped anyone who is going through this annoying issue, as I spent several hours trying to figure it out myself. It seems like Microsoft needs to update their ASP.NET MVC 3 Intranet template to include this line for production scenarios.