ASP.NET Core Identity using JWT Authentication – Detailed Practical Manual
Authentication is one of the most important features in any application. Users need to register, login, and access resources securely.
ASP.NET Core Identity provides a ready-made solution for:
- User Management
- Password Hashing
- Role Management
- Claims Management
- Authorization
- Security Features
Project Structure
A simple project structure:
Required NuGet Packages
Install the following packages.
Identity Package
Purpose:
- IdentityUser
- IdentityRole
- UserManager
- RoleManager
- IdentityDbContext
Entity Framework Core SQL Server
Purpose:
- SQL Server connectivity
EF Core Migration Tools
Purpose:
- Add-Migration
- Update-Database
JWT Authentication Package
Purpose:
- JWT Validation
- Bearer Authentication
Swagger Package
Purpose:
- API Documentation
- JWT Testing
Step 1: Create JWT Settings Model
File Location
Code
Purpose
This class stores JWT configuration values from appsettings.json.
Step 2: Configure appsettings.json
File Location
Code
Purpose
Stores:
- Database Connection String
- JWT Secret Key
- JWT Issuer
- JWT Audience
- Token Expiry Time
Step 3: Create Custom User
File Location
Code
Why Create ApplicationUser?
IdentityUser already contains:
- Id
- UserName
- PasswordHash
- PhoneNumber
ApplicationUser allows us to add extra fields like:
- FirstName
- LastName
- Address
Step 4: Configure Identity in Program.cs
File Location
Identity Registration
Purpose
Registers:
- UserManager
- RoleManager
- Identity Services
Configures password rules.
Step 5: Configure Database
Program.cs
Purpose
Connects Identity to the SQL Server database.
Step 6: Configure JWT Authentication
Program.cs
Purpose
Validates incoming JWT tokens.
Checks:
- Issuer
- Audience
- Secret Key
Step 7: Configure Swagger Authentication
Program.cs
Purpose
Adds the Authorize button inside Swagger UI.
Step 8: Create User API
File Location
Create User Endpoint
What Happens Here?
- User sends data.
- ApplicationUser object is created.
- Identity hashes the password.
- User gets stored in the
AspNetUserstable.
Step 9: Login API
Login Endpoint
What Happens?
- Email searched in the database.
- Password checked against
PasswordHash. - If valid, a JWT token is generated.
Step 10: Generate JWT Token
Login Method
Create Token
Convert to String
Return Token
Step 11: Protect APIs
Admin API
Only users with the Admin role can access.
Manager API
Only users with the Manager role are allowed.
Step 12: Middleware Pipeline
Program.cs
Order is important.
Authentication must come before Authorization.
Step 13: Create Database
Run Migration
Apply Migration
Identity automatically creates:
- AspNetUsers
- AspNetRoles
- AspNetUserRoles
- AspNetUserClaims
- AspNetRoleClaims
- AspNetUserTokens
- AspNetUserLogins
Testing Flow
Register User
POST
Creates a new user.
Login User
POST
Returns:
Swagger Authorization
Click:
Enter:
Now protected APIs can be called.
ASP.NET Core 10.0 Hosting Recommendation
HostForLIFE.eu
HostForLIFE.eu is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.
