Windows Authentication in MVC4 with IIS Express
MVC4 gone through some major changes in Windows Authentication functionality with IIS Express. In this article you will learn how to enable Windows Authentication in MVC4 Web Application on IIS Express, just follow the given steps.
On Cassini web server it was quite difficult to test Windows Authentication as well as it doesn’t support SSL, URL Rewriting Rules etc. With IIS Express as your development server allows you to take full advantage of all web-server features (SSL, URL Rewrite Rules etc). IIS is a full-fledged web-server – which means you’ll get an experience closer to what it will work like when you deploy the application on a production server.
Follow these steps to enable this in MVC4:
Create an MVC Web Application preferably using Internet Application template or Intranet Application template.
Open Web.config file and make following modifications:
<forms loginUrl="~/Account/Login" timeout="2880" />
<authentication mode="Windows" />
I just commented the Forms authentication and added Windows Authentication.
By default MVC apps uses Form Authentication and Simple Membership, so you need to make it ‘false’ in order to run Windows Authentication.
<add key="webpages:Version" value="184.108.40.206" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="autoFormsAuthentication" value="false" />
<add key="enableSimpleMembership" value="false"/>
Select project name in solution explorer and then in the property explorer, click to enable the Windows Authentication.
These settings are called development server settings works with IIS Express and they don’t make any changes in actual configuration settings.
In the property explorer you can disable the Anonymous Authentication if you want your complete website for authenticated users on development server.
If you already disabled the anonymous authentication as suggested in above step 5 you don’t need to do/repeat this step.
If you don’t, let’s go and make any controller action for authorized users, as given below.
Alternatively, you can use [Authorize] action filter with controller directly instead of individual action methods to make every action methods for authorize users.
Notice, in above step I’m using [Authorize] action filter with ‘About’ action. So, when I hit about view page, I’ll be prompted to enter my windows credentials.
When I entered my credentials and hit Login. I will see my windows authentication working.
Further reading, please watch this nice video by Shivprasad Koirala sir (Questpond) https://www.youtube.com/watch?v=x4hzVLZjfLM.
Hope this helps.