Authentication in ASP.Net Core — Getting Started

A step-by-step guide for user authentication in your ASP.Net Core Web Apps

ASP.Net Core Identity Framework

Identity Framework Architecture

#1 — Data Layer

#2 — Data Access Layer

#3 — Business Layer

#4 — Extensions

Enough said, time to code ..

Step #1 — Create a new .Net Core web app project

Step #2 — Add and configure ASP.Net Core Identity in the web app

AppUserStore.cs

Step #3 —User registration

AccountsController.cs
POST request to /accounts/register{
"userName": "john",
"email": "john@email.com",
"password": "&Secret99",
"confirmPassword": "&Secret99"
}
Successful User Registration
POST request to /accounts/register{
"userName": "johndoe",
"email": "johndoe@email.com",
"password": "&Secret99",
"confirmPassword": "&Secret99"
}
Failed User Registration

Step #4 — User login

services.AddAuthentication()
.AddScheme<TOptions, THandler>(string authenticationScheme);
[HttpGet]
[Authorize(AuthenticationSchemes = "myAuth"]
public IActionResult Get()
{
// users are authenticated with "myAuth" scheme ..
// .. registered in the app
}
Startup.cs
Startup.cs
AccountsController.cs
Successful login of the registered user
Cookies in Postman
Log in with the wrong password

Step #5 — User logout

Successful user logout

Demo

app.UseAuthorization();
GET /accounts/details after user login
GET /accounts/details without user login

Software Engineer | Curious Mind

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store