Introduction

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA.

In this post, I specifically talk about.

In this post, I follow the Repository Pattern.

Prerequisites

Step-by-step Implementation

Step 1. Create a new .Net Core 6.0 Web API project.

Web API

New project

Web

Step 2. Project Structure.

Project Structure

Step 3. Install-Package

System.IdentityModel.Token.Jwt

 Install Package

Step 4. Authenticating users using a Web API endpoint.

Implementing the Repository interface

Add Middleware

Valid Token With Role Is Customer

So, at this point, I have authenticated with the Customer Role. I have restricted only Admin User Accessible to get an unauthorized error.

 Customer Role

To Change Admin Role Of testUser Username

TestUser Username