ValidateIssuerSigningKey

ValdiateIssuer

Authority

JwtBearerOptions

ValidIssuer

ValidIssuers

TokenValidationParameters

IssuerSigningKey

certLocation

In my previous posts on the topic of issuing authentication tokens with ASP.NET Core, it was necessary to generate a certificate to use for token signing. As part of that process, a .cer file was generated which contained the public (but not private) key of the certificate. That certificate is what needs to be made available to apps (like this sample) that will be consuming the generated tokens.

With UseJwtBearerAuthentication called in Startup.Configure , our web app should now respect identities sent as JWT bearer tokens in a request’s Authorization header.

Authorizing with Custom Values from JWT

To make the web app consuming tokens a little more interesting, we can also add some custom authorization that only allows access to APIs depending on specific claims in the JWT bearer token.

Role-based Authorization

Authorizing based on roles is available out-of-the-box with ASP.NET Identity. As long as the bearer token used for authentication contains a roles element, ASP.NET Core’s JWT bearer authentication middleware will use that data to populate roles for the user.

So, a roles-based authorization attribute (like [Authorize(Roles = "Manager,Administrator")] to limit access to managers and admins) can be added to APIs and work immediately.

Custom Authorization Policies

Custom authorization in ASP.NET Core is done through custom authorization requirements and handlers. ASP.NET Core documentation has an excellent write-up on how to use requirements and handlers to customize authorization. For a more in-depth look at ASP.NET Core authorization, check out this ASP.NET Authorization Workshop.

The important thing to know when working with JWT tokens is that in your AuthorizationHandler ‘s HandleRequirementAsync method, all the elements from the incoming token are available as claims on the AuthorizationHandlerContext.User . So, to validate that a custom claim is present from the JWT, you might confirm that the element exists in the JWT with a call to context.User.HasClaim and then confirm that the claim is valid by checking its value.

Again, details on custom authorization policies can be found in ASP.NET Core documentation, but here’s a code snippet demonstrating claim validation in an AuthorizationHandler that authorizes users based on the (admittedly strange) requirement that their office number claim be lower than some specified value. Notice that it’s necessary to parse the office number claim’s value from a string since (as mentioned in my previous post), ASP.NET Identity stores all claim values as strings.

// A handler that can determine whether a MaximumOfficeNumberRequirement is satisfied internal class MaximumOfficeNumberAuthorizationHandler : AuthorizationHandler < MaximumOfficeNumberRequirement > { protected override Task HandleRequirementAsync ( AuthorizationHandlerContext context , MaximumOfficeNumberRequirement requirement ) { // Bail out if the office number claim isn't present if (!context.User.HasClaim(c => c.Issuer == " http://localhost:5000/ " && c.Type == " office " )) { return Task.CompletedTask; } // Bail out if we can't read an int from the 'office' claim int officeNumber ; if (! int .TryParse(context.User.FindFirst(c => c.Issuer == " http://localhost:5000/ " && c.Type == " office " ).Value, out officeNumber)) { return Task.CompletedTask; } // Finally, validate that the office number from the claim is not greater // than the requirement's maximum if (officeNumber <= requirement.MaximumOfficeNumber) { // Mark the requirement as satisfied context.Succeed(requirement); } return Task.CompletedTask; } } // A custom authorization requirement which requires office number to be below a certain value internal class MaximumOfficeNumberRequirement : IAuthorizationRequirement { public MaximumOfficeNumberRequirement (int officeNumber) { MaximumOfficeNumber = officeNumber; } public int MaximumOfficeNumber { get ; private set ; } }

This authorization requirement can be registered in Startup.ConfigureServices with a call to AddAuthorization to add a requirement that an office number not exceed a particular value (200, in this example), and by adding the handler with a call to AddSingleton :

// Add custom authorization handlers services.AddAuthorization (options => { options.AddPolicy( " OfficeNumberUnder200 " , policy => policy.Requirements.Add( new MaximumOfficeNumberRequirement (200))); }); services.AddSingleton < IAuthorizationHandler , MaximumOfficeNumberAuthorizationHandler>();

Finally, this custom authorization policy can protect APIs by decorating actions (or controllers) with appropriate Authorize attributes with their policy argument set to the name used when defining the custom authorization requirement in startup.cs:

[Authorize(Policy = " OfficeNumberUnder200 " )]

Testing it All Together

Now that we have a simple web API that can authenticate and authorize based on tokens, we can try out JWT bearer token authentication in ASP.NET Core end-to-end.

The first step is to login with the authentication server we created in my previous post. Once that’s done, copy the token out of the server’s response.

Now, shut down the authentication server just to be sure that our web API can authenticate without it being online.

Then, launch our test web API and using a tool like Postman or Fiddler, create a request to the web API. Initially, the request should fail with a 401 error because the APIs are protected with an [Authorize] attribute. To make the calls work, add an Authorization header with the value “bearer X” where “X” is the JWT bearer token returned from the authentication server. As long as the token hasn’t expired, its audience and authority match the expected values for this web API, and the user indicated by the token satisfies any custom authorization policies on the action called, a valid response should be served from our web API.

Here are a sample request and response from testing out the s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Transfer-Encoding: chunked Content-Type: text/plain; charset=utf-8 Server: Kestrel value

Conclusion

As shown here, authenticating using JWT bearer tokens is straightforward in ASP.NET Core, even in less common scenarios (such as the authentication server not being available). What’s more, ASP.NET Core’s flexible authorization policy makes it easy to have fine-grained control over access to APIs. Combined with my previous posts on issuing bearer tokens, you should have a good overview of how to use this technology for authentication in ASP.NET Core web apps.

Resources