alex-toy / AspNetCoreRestAPIAuthorizationwithJWTRolesClaimsPolicyStepbyStep

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Asp Net Core - Rest API Authorization with JWT (Roles Vs Claims Vs Policy)

ASP.NET Core authorization provides a simple, declarative role and a rich policy-based model. Authorization is expressed in requirements, and handlers evaluate a user's claims against requirements. Imperative checks can be based on simple policies or policies which evaluate both the user identity and properties of the resource that the user is attempting to access.

Migration

In Package Manager Console :

Add-Migration Add_refresh_token_table
Update-Database

Refresh Token

Register

Login

We can retrieve the meaning of the token. Currently we don't have the roles included in the token :

Refresh

Setup Controller

Get Roles

Get Users

Add User to Role

Add roles to token

Get Users

Remove User from Role

Authentication Controller

Register new User

  • Register :

  • Get user role :

  • Token not yet expired :

  • Token has been used :

  • Token successfully refreshed :

To Do Controller

  • Todo without authentication :

  • Todo with authentication :

  • Remove user from role :

  • User cannot create todo because lacking required AppUser role :

ClaimsSetup Controller

Policy

  • user not authorized because lacking Department policy :

  • Add department policy to that user :

  • now the user is authorized :

About


Languages

Language:C# 100.0%