Back to: ASP.NET Web API Tutorials For Begineers and Professionals
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 in order to create a new ASP.NET MVC Application as shown in the below image.
Step 2: Select ASP.NET Web Application from the New Project window and enter a name for the project such as AspNetIdentityWithNewProject and then click on the OK button as shown in the below image.
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.
Step 4: In order to implement ASP.NET Identity by default in this new project, click the Change Authentication button as shown in the below image.
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.
Then finally click on the OK button as shown in the below image. Here, the MVC Project Template and Individual User Accounts Authentication type must be selected.
Once you click on the OK button Visual Studio will create the Project for us which will automatically include 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, then you will see the following important files created by visual studio.
Following are the important class files:
- IdentityConfig.cs is primarily used to configure the user manager and the sign-in manager of the application.
- Startup.Auth.cs is used to configure authentication.
- IdentityModels.cs is used to customize user profiles.
- Startup.cs contain the Startup class which is the entry point of the application.
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, what you need to do is, create a new Application with ASP.NET Identity and then copy and paste the above four class files into their respective folders.
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.
Step 2: Select ASP.NET Web Application from the New Project window and enter a name for the project such as AspNetIdentityWithExistingProject and then click on the OK button as shown in the below image.
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.
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.
Step 2: From the NuGet Package Manager window, select the Browse tab, then search for Microsoft.AspNet.Identity.EntityFramework package. Then select Microsoft.AspNet.Identity.EntityFramework package and also select the Project where you want to install ASP.NET Identity and click on the Install button as shown in the below image.
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.
Step 3: Search for and install Microsoft.AspNet.Identity.Owin package by following the same steps as shown in the below image.
Accept the License as shown in the below image.
Step 4: Search for and install Microsoft.Owin.Host.SystemWeb package by following the same steps as shown in the below image.
Accept the License as shown in the below image.
With the above packages, the ASP.NET Identity is installed on our existing project.
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 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.
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 am going to explain each and every 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. Here, 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 Getting Started with ASP.NET Identity with New and Existing Project article.