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.
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:
Select Web Application (Model-View-Controller) and click on Change Authentication
Select Individual User Accounts and click ok to create your project.
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.
"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:
After using above commond, check your database will be updated.
Now Run the application and register a New User.
After Registering, we can User logging.
We can see registered user stored in database;
Use the following command to see users from database table in sql server:
select * from AspNetUsers
I hope it will help to you after reading it.