Getting Started with ASP.NET Identity

Getting Started with ASP.NET Identity:

In this article, I will show you How to get started with ASP.NET Identity both in a new Project and in an Existing Project. Please read our previous article, where we discussed ASP.NET Identity Architecture.

ASP.NET Identity with New Project

Let us see the step-by-step process to create a new ASP.NET MVC Application using ASP.NET Identity.

Step 1: Please select File > New > Project to create a new ASP.NET MVC Application, as shown in the image below.

ASP.NET Identity with New Project

Step 2: Select ASP.NET Web Application from the New Project window and enter a name for the project, such as AspNetIdentityWithNewProject. Then click on the OK button, as shown in the image below.

ASP.NET Web Application

Step 3: Check the MVC checkbox from the New ASP.NET Web Application window, as shown in the below image. But do not click on the OK button.

ASP.NET Web Application window

Step 4: To implement ASP.NET Identity by default in this new project, click the Change Authentication button, as shown in the image below.

Change Authentication

Then select the Individual User Accounts from the Change Authentication window and click on the Ok button from the Change Authentication window, as shown in the below image.

Individual User Accounts

Then finally, click on the OK button as shown in the below image. The MVC Project Template and Individual User Accounts Authentication type must be selected here.

MVC Project Template and Individual User Accounts Authentication

Once you click the OK button, Visual Studio will create the Project for us, automatically including all the required files needed to use ASP.NET Identity features. Visual Studio will also provide a project template that will make it easier for us to get started with this membership system. If you expand the Project Folders, you will see the following important files created by the visual studio.

ASP.NET Identity features
Following are the Important Class Files:

  1. IdentityConfig.cs is primarily used to configure the application’s user and sign-in manager of the application.
  2. Startup.Auth.cs is used to configure authentication.
  3. IdentityModels.cs is used to customize user profiles.
  4. Startup.cs contain the Startup class, which is the application’s entry point.

If this is not clear at the moment, then don’t worry. In our upcoming articles, we will discuss these files in detail. So, this is how you can integrate ASP.NET Identity with a New Project.

Note: Next, we are going to discuss how to add ASP.NET Identity with an existing ASP.NET MVC or ASP.NET Web API Project. Before implementing ASP.NET Identity with an Existing Project, let me tell you one thing, the above four class files (IdentityConfig.cs, Startup.Auth.cs, IdentityModels.cs, Startup.cs) are not going to be included by default with an existing project. So, you need to create a new Application with ASP.NET Identity and then copy and paste the above four class files into their respective folders.

Packages for ASP.NET Identity

The created project contains the following three packages for ASP.NET Identity.

  1. Microsoft.AspNet.Identity.EntityFramework: This package includes the Entity Framework implementation of ASP.NET Identity, which enables the persistence of ASP.NET Identity data and schema to SQL Server database.
  2. Microsoft.AspNet.Identity.Core: This particular package contains the essential interfaces required for ASP.NET Identity. Its main function is to enable the development of ASP.NET Identity implementations that target different persistence stores, such as NoSQL databases and Azure Table Storage.
  3. Microsoft.AspNet.Identity.OWIN: The package “Microsoft.AspNet.Identity.OWIN” provides the necessary tools to incorporate OWIN authentication with ASP.NET Identity in ASP.NET applications. It is particularly useful when adding sign-in functionality to your application and utilizing OWIN Cookie Authentication.

ASP.NET Identity with Existing Project

The ASP.NET Identity is available as a NuGet package, so we will use NuGet Package Manager in order to implement ASP.NET Identity in an existing project. So, let us proceed and see the step-by-step process.

Step 1: First, Create a New ASP.NET MVC Application. To do so, select File > New > Project, as shown in the below image.

ASP.NET Identity with Existing Project

Step 2: Select ASP.NET Web Application from the New Project window and enter a name for the project, such as AspNetIdentityWithExistingProject. Then click on the OK button, as shown in the image below.

ASP.NET Web Application

Step 3: Then check the MVC checkbox from the New ASP.NET Web Application window as shown in the below image. And click on the OK button. Here, you can see we are using No Authentication as the Authentication type, which means we are creating the project without ASP.NET Identity. Later we will see how to add ASP.NET Identity to this Project.

ASP.NET Identity to this Project

Once you click on the OK button, the ASP.NET MVC Web Application will be created for us by Visual Studio without using ASP.NET Identity. So, we are ready with our existing project, which does not have ASP.NET Identity. Now, let us proceed and see the steps to add ASP.NET Identity with our existing ASP.NET MVC Project.

Adding ASP.NET Identity with Existing Project:

Step 1: Open NuGet Package Manager to add ASP.NET Identity. To do so, select Tools > NuGet Package Manager > Manage NuGet Packages for Solution option, as shown in the below image.

Adding ASP.NET Identity with Existing Project

Step 2: Select the Browse tab from the NuGet Package Manager window, then search for Microsoft.AspNet.Identity.EntityFramework package. Then select Microsoft.AspNet.Identity.EntityFramework package and select the Project where you want to install ASP.NET Identity and then click on the Install button as shown in the image below.

Microsoft.AspNet.Identity.EntityFramework

Note: The Microsoft.AspNet.Identity.EntityFramework package will also install the dependency packages: Microsoft.AspNet.Identity.Core and EntityFramework.

Once you click on the Install button, it will start installing the ASP.NET Identity. While installing ASP.NET Identity, one pop-up will open and ask you to accept the License. Simply click on the I Accept button as shown in the below image.

Microsoft.AspNet.Identity.EntityFramework package

Step 3: Search for and install Microsoft.AspNet.Identity.Owin package by following the same steps as shown in the below image.

Microsoft.AspNet.Identity.Owin package

Accept the License as shown in the below image.

Accept the License

Step 4: Search for and install Microsoft.Owin.Host.SystemWeb package by following the same steps as shown in the below image.

Microsoft.Owin.Host.SystemWeb package

Accept the License as shown in the below image.

Microsoft.Owin.Host.SystemWeb Package

The ASP.NET Identity is installed on our existing project with the above packages.

Step 5: Download the Following Files:

By default, IdentityConfig.cs, Startup.Auth.cs, IdentityModels.cs, and Startup.cs class files are not created for us when we add ASP.NET Identity with an existing ASP.NET MVC or ASP.NET Web API Project. So, what we will do is we will copy the above files from our previous project, where we created a new ASP.NET MVC Project with ASP.NET Identity.

IdentityConfig.cs and Startup.Auth.cs:

Copy the above two files from the App_Start folder of our previous project (AspNetIdentityWithNewProject) and then paste these two class files within the App_Start folder of AspNetIdentityWithExistingProject.

IdentityModels.cs

Copy the IdentityModels.cs class file from the Models folder of our previous project (AspNetIdentityWithNewProject) and then paste this class file within the Models folder of AspNetIdentityWithExistingProject.

Startup.cs

Copy the Startup.cs class file from the root directory of our previous project (AspNetIdentityWithNewProject) and then paste this class file into our root directory of AspNetIdentityWithExistingProject.

With the above files in place with our existing project, our AspNetIdentityWithExistingProject structure should look as shown below.

How to Get Started with ASP.NET Identity with New and Existing Projects

Note: One more change we need to do is, change the namespace in the IdentityConfig.cs, Startup.Auth.cs, IdentityModels.cs, and Startup.cs files with your project namespace, i.e., replace AspNetIdentityWithNewProject with AspNetIdentityWithExistingProject.

Note: We are going to use these two projects throughout this course. So, basically, I will explain each concept using new Projects and existing Projects with ASP.NET Identity. This is because sometimes you need to start developing from scratch where you can implement ASP.NET Identity. Sometimes, you need to work with an existing project where you need to implement ASP.NET Identity.

In the next article, I am going to discuss Setting up ASP.NET Identity with EF Code-First Approach. In this article, I try to explain How to Get Started with ASP.NET Identity with New and Existing Projects. I hope you enjoy this article Getting Started with ASP.NET Identity with New and Existing Projects.

Leave a Reply

Your email address will not be published. Required fields are marked *