JWT authentication with Identity
In this recipe, we’ll implement JWT authentication. While cookie-based authentication is suitable for many scenarios, JWT offers a stateless alternative. One nice thing about working with JWT is we only have to concern ourselves with the token itself and not a cookie tied to a session. Also, cookies present many security challenges if we have an API that eventually has to work across multiple domain names. JWT allows the API to authenticate users without having to maintain the session state on the server. JWT is becoming generally preferred over authentication cookies.
Getting ready…
The starter project for this recipe can be found here: https://github.com/PacktPublishing/ASP.NET-9-Web-API-Cookbook/tree/main/start/chapter03/JWTAuth
The starter project is similar to the preceding recipe but with the cookie authentication removed.
A terminal with PowerShell is recommended for following along.
How to do it…
- Install...