Authentication in ASP.Net MVC 5 using Identity Libraries - Part 1
In my earlier post I had referred to the post by Ben Foster on using ASP.Net Identity libraries to add authentication support.
I’m planning to try the same using an ASP.Net MVC 5 web application.
I’ll be covering the same on my blog as a 3 part series, this being the first part.
The aim is to add cookie based authentication on the lines of ASP.Net forms based authentication.
We’ll achieve this using the ASP.Net Identity libraries.
With this background, lets begin…
- Create an empty ASP.Net MVC 5 project.
- Go to
File -> New -> Project
in Visual Studio. (Note: I’m using Visual Studio 2015) - Select the
ASP.Net Web Application
project - On the ASP.Net template dialog select the
Empty
template. - In the
add folders and core references
section select theMVC
option.
- Go to
- This will create the basic ASP.Net MVC 5 web project.
- To perform cookie based authentication we need to install the following 2 Nuget packages
- Microsoft.Owin.Host.SystemWeb
- This package enables the OWIN middleware to hook into the IIS request pipeline.
- Install this using
Install-Package Microsoft.Owin.Host.SystemWeb
- Microsoft.Owin.Security.Cookies
- This package enables cookie based authentication.
- Install this using
Install-Package Microsoft.Owin.Security.Cookies
- Microsoft.Owin.Host.SystemWeb
- It’s now time to initialize the OWIN identity components.
- Add a
Startup.cs
class to the project. - Add the
Configuration
method which accepts theIAppBuilder
instance as a parameter as follows…
- Add another class file
Startup.Auth.cs
underApp_Start
folder. - Create a partial class
Startup
in this file. - To add cookie based authentication we can now add…
- Here the
UseCookieAuthentication
extension tells ASP.Net Identity framework to use cookie based authentication.- AuthenticationType
This is a string value that identifies the cookie.
Note: If we installMicrosoft.AspNet.Identity
Nuget package we can use the constantDefaultAuthenticationTypes.ApplicationCookie
which has the same value asApplicationCookie
. - LoginPath
This is set to the path to which the browser should be redirected, when your application returns an unauthorized response (HTTP 401).
In our case we have anAuthController
with aLogin
action.
- AuthenticationType
- In the
Startup
class in the root folder place a call to theConfigureAuth
method as follows…
- Before we add the authentication logic let us first create a basic resource for which we plan to have an authorized access.
- Lets add the following controller
- and the corresponding view as…
- The
RouteConfig
class in RouteConfig.cs file underApp_Start
folder has the following default route…
- Hence if we now run the web application it will display this HomeController’s index page as follows…
- We’ll now create the
AuthController
. - This will have the
Login
action to which the user would be re-directed when they need to log in.
- and the corresponding view as…
- Now lets decorate the
HomeController
with the[Authorize]
attribute as we want only authorized users to be able to access it.
- Now run the application.
You’ll observe that when the Default route is requested which is
http://localhost:51910/
instead of opening up theIndex
view of theHomeController
it gets redirected toLogin
action of theAuthController
. -
The redirected url now shows up as…
http://localhost:51910/auth/login?ReturnUrl=%2F
- Also, the
Login
view of theAuthController
shows up in the browser.
Next we’ll cover the actual log in logic in Part 2 of this series.
Leave a Comment