Adding claims that can be used for authorization

JWT allows you to set whatever key-value you wish as a claim. We can leverage this fact to store user permissions so that we can later retrieve them and use them for authorization. The ASP.NET Core authorization infrastructure provides us with an easy way to add claims to the user and store them in the database by giving us the AddClaimsAsync method that's provided by the UserManager class. For example, I modified the GiveNTake user registration logic to add the registration date as a claim:

[AllowAnonymous][HttpPost("register")]public async Task<IActionResult> Register([FromBody] RegisterUserDTO registration){    ...      user = await _userManager.FindByEmailAsync(registration.Email); await _userManager.AddClaimAsync(user, ...

Get Hands-On Full-Stack Web Development with ASP.NET Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.