Implement Authentication Using Identity Model In ASP.NET Core

ASP.NET Core Identity is a membership system that adds login functionality to ASP.NET Core apps. Users can create an account with the login information stored in Identity or they can use an external login provider.

Supported external login providers include Facebook, Google, Microsoft Account, and Twitter.

Identity can be configured using a SQL Server database to store user names, passwords, and profile data. Alternatively, another persistent store can be used, for example, Azure Table Storage.

Open Visual Studio 2017 and let’s create a new ASP.NET Core Web Application project. to do this, Select File > New > Project. In the New Project dialog select Visual C# > Web > ASP.NET Core Web Application just like in the figure below:

Implement Authentication Using Identity Model In ASP.NET Core

Name your project to whatever you like, but for this exercise, we will name it as “AspDotNetCoreMvc” to confirm with the topic. Click OK and then select “Web Application (Model-View-Controller)” within ASP.NET Core templates as shown in the following figure below:

Implement Authentication Using Identity Model In ASP.NET Core

Now click on Change Authentication box.

Select the individual User Accounts options and then click the OK button to let Visual Studio generate the required files needed for us to run the application. The figure below shows the default generated files:

Implement Authentication Using Identity Model In ASP.NET Core

Now, open the appsettings.json file and provide the database name, database server name, and related credentials to establish the connection with the database.

{
  "ConnectionStrings": {
    "DefaultConnection": "Password=sa@123;Persist Security Info=True;User ID=sa;Initial Catalog=TechMindCore;Data Source=(local)"
  },
  "Logging":
{
    "IncludeScopes": false,
    "LogLevel":
{
      "Default":
"Warning"
    }
  }
}

Now, open the Package Manager Console from the Tools menu and run the below commands one by one.

PM> Update-Database

After successfully running the above commands, go to the SQL Server and check that related database has been created.

Implement Authentication Using Identity Model In ASP.NET Core

Now, build and run the applications. After running the application in the browser, click on the "Sign In" button.

 Implement Authentication Using Identity Model In ASP.NET Core

Since we do not have any login credentials, we need to register first and then try to log in. For that, click on the link Register as a New User.

 Implement Authentication Using Identity Model In ASP.NET Core

In the Registration form, provide a Username and Password to register.

After successful registration, the application allows us to log into the application.

 Implement Authentication Using Identity Model In ASP.NET Core

Now, return back to the SQL server and run a query to select records from the table dbo.AspNetUsers

This Table contains the register user details as below.

Implement Authentication Using Identity Model In ASP.NET Core

 Now, in this article, we discussed the basic concepts of Identity-based Authentication in ASP.NET Core. I hope, this will help the readers to understand how to implement identity-based authentication in any application. Please leave your feedback using the comments which helps me to improve myself for the next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks.

Read CRUD Operation With Razor Pages In ASP.NET Core MVC

  Modified On Dec-24-2019 06:53:59 AM

Leave Comment