ASP.Net Core JWT to assist with JWT authentication/authorization for Web API when using Entity Framework Core Identity
- ASP.Net Core 2.1
- Microsoft.AspNetCore.Authentication.JwtBearer
- System.IdentityModel.Tokens.Jwt
- Install NuGet package
- Initialize JWT in
Startup.ConfigureServices
. For example (Sampe Startup):
services.AddJwtBearerAuthentication(
Configuration["Authentication:TokenSigningKey"],
Configuration["Authentication:Issuer"],
Configuration["Authentication:Audience"],
60,
1440);
- Inject
TokenBuilder
class into appropriate controller or class (AddJwtBearerAuthentication
creates transient dependency rule for this class) -- TheGenerateToken
method creates a JWT signing token
The BaseAuthorizationHandler abstrat class assists with the authorization policies.
It collects the various segments of the API URL so that you can validate them against authenticated user's claims.
See the OwnUserHandler in the sample code for an example implementation.