Understanding Asp DotNet Identity

Facebook Twitter Google Digg Reddit LinkedIn Pinterest StumbleUpon Email


This is the first of two series blog post that explains how to work with the new Asp .Net Identity framework. If you have used/worked with Asp .Net MembershipProvider, RoleProvider and ProfileProvider, this should sound very familiar to you. However there are major underlying changes on how you handle use Identity going forward in any .Net stack application being a web site or web services or mobile applications. But if you are new to this frameworks, this just a default user management mechanism going forward.

Alright, so let's jump right into the code.

Create Empty MVC application

Asp .Net Identity

Asp .Net Identity

Adding OWIN & Web Host Packages

Next, we need to add bunch of OWIN and Web Host packages for our application. Let's go ahead and following packages. Search for OWIN security in the Nuget Package Manager and install the selected packages. Watch for the dependency packages also.

Asp .Net Identity

This should install following packages in the project.

  • Microsoft.Owin
  • Microsoft.Owin.Host.SystemWeb
  • Microsoft.Owin.Security
  • Microsoft.Owin.Security.Cookies

Next, let's setup the OWIN Startup class. OWIN defines abstraction between the Web Server and the web application. This gives you an option to create middleware for .Net based web applications. Also decoupling from web server meaning the application can be ported from IIS host to different self-hosting options.

Add new OWIN Startup Class.

Asp .Net Identity

This is where we will configure our middleware specifically, the UserStore and Authentication option.

Adding Asp .Net Identity Packages

Now that OWIN is setup in the project, let's start adding packages for Authentication middleware which is Asp .Net Identity. Search for Identity and install following packages. Also watch for the dependency packages being installed as part of one or more of these packages.

Asp .Net Identity

  • Microsoft.AspNet.Identity.Core
  • Microsoft.AspNet.Identity.EntityFramework
  • Microsoft.AspNet.Identity.Owin

Now that we have packages, let's create couple of things. First we need the ApplicationUserManage that we can provide to our OWIN middleware. And the ApplicationManagerUser will need the instance of the DbContext that Asp .Net Identity will use for all user authentication and registration related operations.

So let's add IdentityConfig class in App_Start Folder and in that create our ApplicationUserManage which derives from UserManger class from,

Microsoft.AspNet.Identity

Asp .Net Identity

Asp .Net Identity

The UserManager will need the type of user it is going to maintain so let's also add a class ApplicationUser which will the inherited class from IdentityUser. This class will help generating the Identity of the user with defined Authentication type. In our case it will be ApplicationCookie.

Asp .Net Identity

Next, let's add our UserDbContext class which will be the EntityFramework IdentityDbContext class for managing user related database operations.

Asp .Net Identity

Asp .Net Identity

Let's also define the connection string with name Identityconnection to our target database where we want to store user accounts.

Asp .Net Identity

Now let's revisit the ApplicationUserManager class and define few validation rules that we were used to define in Web.Config in old days of Membership provider.

Asp .Net Identity

We are configuring the user,password validation rules and also adding the UserTokenProvider.

And finally let's put all of this to our OWIN Startup class so as soon as the application starts all the things will be setup.

Asp .Net Identity

Here we are initializing the UserDbContext, ApplicationManager and setting the CookieAuthenticationOptions for our application.

All right. So looks like we have everything we need in place fore Asp .Net Identity. All we need to do now is create a registration page and use these Identity framework to Register & Authenticate user. We will see that in the next blog.