In this article, I am going to explain how to implement ASP.NET Identity using OWIN middleware components for authentication, including support for log-ins using external identity providers (like Microsoft Accounts, Facebook, Google, Twitter), and log-ins using organizational accounts from on-premises Active Directory. OWIN also support for OAuth.

ASP.NET Identity is new approach to implement membership in ASP.NET application and now part of new ASP.NET MVC 5 and web form project. ASP.NET Identity targets version 4.5 of the .NET Framework. That means, when you create MVC 5 application Identity will be automatically added into your project. In this article, I am going to demonstrate the necessary steps required to implement ASP.NET Identity framework in MVC 5 Web Pages application. To know more about ASP.NET Identity, read my previous article "Understanding ASP.NET Identity"

Start Creating New ASP.NET MVC 5 Empty Project

The first step in this walk through involves creating new MVC empty project. So let's open Visual Studio and start creating new MVC 5 project. In this article, I am using Visual Studio Express 2013 for Web but you can use Visual Studio 2013 as well. So start creating ASP.NET MVC 5 project in Visual Studio 2013.

By default, ASP.NET Identity uses SQL Server database. But you have greater flexibility to change with other database such as Oracle, MySQL, SharePoint, NoSql and any other database as well.

Step 1: Start Visual studio 2013, click on New Project from File menu and select Web and then ASP.NET web application from New Project. I am selecting .Net framework 4.5 to build this sample application. Now give proper name as ASPNETMVCIdentity to your application and click Ok button. Select Empty and checked MVC then click on "Ok" button as bellow screen shot illustrate:

Step 2: Now, you will need to add below listed packages that will help you to implement ASP.NET Identity in your application. All right, right click on project or go to Tool menu and select Library Package Manager and then select Manage NuGet Package for Solution... option.

Step 3: Now, add new class file in Models folder and name as IdentityModels.cs. You can observe that LoginUser class is implementing IdentityUser interface. The LoginUser class includes other properties which are reflected in the database schema that Entity Framework will generate when the application first runs.
In order to use Entity Framework code first, you will need to create "DbContext" class that derives from "IdentityDbContext" class by passing LoginUser class.
Add below code in newly added class file.

Adding RegisterViewModel

Step 1: Now, right click on Models folder and add new class name as RegisterViewModel.cs. Now add user profile properties that you want to capture in registration page. Also add required validation as per your requirement. Remember, you need to add System.ComponentModel.DataAnnotations namespace which is required to add validation in model.

Adding LoginViewModel

Step 1: Now, right click on Models folder and add new class name as LoginViewModel.cs. Now add user LOGIN properties that you want to use in login page. Also add required validation as per your requirement. Remember, you need to add System.ComponentModel.DataAnnotations namespace which is required to add validation in model.

Adding IdentityConfig

Step 1: Now, right click on App_Start folder and add new class name as IdentityConfig.cs. Add two classes IdentityUserStore and IdentityUserManagerThe IdentityUserStore class implanting UserStore class which is an Entity Framework specific class that provides data access methods for communicating with the underlying SQL Server database. On the other hand, The IdentityUserManager class implemting UserManager class which is Entity Framework's that including methods for managing users.

Adding Home Controller

Step 1: Now, right click on Controllers folder and add new controller name as HomeController.cs. In order to receive posted user data, you will need to add methods in HomeController class. An instance of a UserManager is created in the code. The UserManager.Create method is called to create new user which return type of this method is an instance of the IdentityResult class. It has a Succeeded property which is a bool, and indicates that whether operation was successful or not.
Replace below code in HomeController.cs class.

Authenticating Users

Step 1: Now, add new class file Owin StartUp class in your project and name as StartUp.cs.
In the MVC 5 templates, the StartUp class is used to wire up the authentication middleware which is all built on top of OWIN.
The StartUp class is basically convention for Katana/OWIN to initialize the pipeline. When you run your application, the code inside of the StartUp class will run to set up the components that will be used. Add below code in newly added class file.

Adding Connection String

Run Application

Step 1: All right, now your application is ready to run. Enter required fields in registration page and click on Register button. After successful registration you will redirected to login page with registered user name and SignOut link i.e. "Jimi" in example code.

Appreciate your valuable feedback:

I hope this article is useful for you. I look forward for your comments and feedback. So please provide your valuable feedback so that I can make this blog better. You can also share this article by hitting below button. Happy learning...