By Damien Guard
This tutorial demonstrates how to add authorization to an ASP.NET Core Web API application using the standard JWT middleware.We recommend that you log in to follow this quickstart with examples configured for your account.New to Auth0? Learn how Auth0 works and read about implementing API authentication and authorization using the OAuth 2.0 framework.
Configure Auth0 APIs
Create an API
In the APIs section of the Auth0 dashboard, click Create API. Provide a name and an identifier for your API, for example,https://quickstarts/api
. You will use the identifier as an audience
later, when you are configuring the Access Token verification. Leave the Signing Algorithm as RS256.

Define permissions
Permissions let you define how resources can be accessed on behalf of the user with a given access token. For example, you might choose to grant read access to themessages
resource if users have the manager access level, and a write access to that resource if they have the administrator access level.
You can define allowed permissions in the Permissions view of the Auth0 Dashboard’s APIs section.

This example uses the
read:messages
scope.- How to check for a JSON Web Token (JWT) in the
Authorization
header of an incoming HTTP request. - How to check if the token is valid, using the JSON Web Key Set (JWKS) for your Auth0 account. To learn more about validating Access Tokens, see Validate Access Tokens.
Configure the Sample Project
The sample code has anappsettings.json
file which configures it to use the correct Auth0 Domain and API Identifier for your API. If you download the code from this page while logged in, it will be automatically filled. If you use the example from Github, you will need to fill it yourself.
Validate Access Tokens
Install dependencies
The seed project already contains a reference to theMicrosoft.AspNetCore.Authentication.JwtBearer
, which is needed in order to validate Access Tokens.
However, if you are not using the seed project, add the package to your application by installing it using Nuget:
Configure the middleware
The ASP.NET Core JWT Bearer authentication handler downloads the JSON Web Key Set (JWKS) file with the public key. The handler uses the JWKS file and the public key to verify the Access Token’s signature. In your application, register the authentication services:- Make a call to the
AddAuthentication
method. ConfigureJwtBearerDefaults.AuthenticationScheme
as the default schemes. - Make a call to the
AddJwtBearer
method to register the JWT Bearer authentication scheme. Configure your Auth0 domain as the authority, and your Auth0 API identifier as the audience. In some cases the access token will not have asub
claim which will lead toUser.Identity.Name
beingnull
. If you want to map a different claim toUser.Identity.Name
then add it tooptions.TokenValidationParameters
within theAddAuthentication()
call.
UseAuthentication
and UseAuthorization
methods in your Program.cs file:
Validate scopes
To make sure that an Access Token contains the correct scope, use the Policy-Based Authorization in ASP.NET Core. Create a new authorization requirement calledHasScopeRequirement
. This requirement checks if the scope
claim issued by your Auth0 tenant is present. If the scope
claim exists, the requirement checks if the scope
claim contains the requested scope.
AddAuthorization
method. To add policies for the scopes, call AddPolicy
for each scope. Also ensure that you register the HasScopeHandler
as a singleton:
Protect API Endpoints
The JWT middleware integrates with the standard ASP.NET Core Authentication and Authorization mechanisms. To secure an endpoint, you need to add the[Authorize]
attribute to your controller action:
access_token
. To do that, add the Authorize
attribute to the Scoped
action and pass read:messages
as the policy
parameter.