How to use Authentication in ASP.NET Core MVC

How to use Authentication in ASP.NET Core MVC

Introduction:

In this article we are going to explain how to use authentication in mvc core or how to create individual authentication in mvc core or how to use ASP.NETCore Identity in MVC Application with example.

Description:

ASP.NET Mvc Core Identity allows us to add login functionality to our system. Here, in this demo, we will be using SQL Server to store the user details and profile data. We will use ASP.NET Identity for new user registration, login, and to maintain the user profile data. If we talk about the login, the important part is whether the logged in user is authenticated and also authorized to view the pages.

Let’s see how to create a simple Login and Register Functionality in ASP.NET Mvc Core.

 Step 1: We will create a Database and set the connection string in appsettings.json file for DefaultConnection with our new database connection. We will be using this database for ASP.NET Core Identity table creation.

Create Database: Run the following script to create our database.

Create DataBase TechMindCore

Step 2: Create your ASP.NET Core

After installing our Visual Studio 2017, click Start, then Programs and select Visual Studio 2017 - Click Visual Studio 2017. Click New, then Project, select Web and then select ASP.NET Core Web Application. Enter your project name and click: 

How to use Authentication in ASP.NET Core MVC

Select Web Application (Model-View-Controller) and click on Change Authentication

How to use Authentication in ASP.NET Core MVC

Select Individual User Accounts and click ok to create your project.

How to use Authentication in ASP.NET Core MVC

Updating appsettings.json

In appsettings.json file we can find the DefaultConnection Connection string. Here in connection string change your SQL Server Name, UID and PWD to create and store all user details in one database.

"ConnectionStrings":

{
    "DefaultConnection": "Password=sa@123;Persist
Security Info=True;User ID=sa;Initial Catalog=TechMindCore;Data
Source=(local)"
  }

Using Package Manager Console 

Go to Tools –> NuGet Package Manager –> Package Manager Console 

And then run the following command below to update database: 

PM> Update-Database

  After using above commond, check your database will be updated.

How to use Authentication in ASP.NET Core MVC

Now Run the application and register a New User.

How to use Authentication in ASP.NET Core MVC

After Registering, we can User logging.

How to use Authentication in ASP.NET Core MVC

We can see registered user stored in database;

Use the following command to see users from database table in sql server:

select * from AspNetUsers

How to use Authentication in ASP.NET Core MVC

I hope it will help to you after reading it.

Last updated:12/1/2020 3:59:02 AM
Aditya Patel

Aditya Patel

Hi, I am a Senior Software Developer in MindStick Software pvt. Ltd. I have done BCA from dr. c.v. Raman university. I have more than five years experience in Microsoft Technology MVC 4, MVC Core, C#,Vb.Net, rdlc Reporting ,SQL Server and other jQuery, css, Bootstrap, Knockout.js, angular.js,react.js Html, Crystal Reporting etc.

0 Comments

Leave Comment