ASP.NET Core Web API v2.0: Authorization
This tutorial demonstrates how to add authorization to an ASP.NET Core Web API using the standard JWT middleware. We recommend you to Log in to follow this quickstart with examples configured for your account.
I want to integrate with my app15 minutes
I want to explore a sample app2 minutes
Get a sample configured with your account settings or check it out on Github.
What is cross-origin authentication?
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.
By default, your API uses RS256 as the algorithm for signing tokens. Since RS256 uses a private/public keypair, it verifies the tokens against the public key for your Auth0 account. The public key is in the JSON Web Key Set (JWKS) format, and can be accessed here.
Configure your application for cross-origin authentication
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 the
messages 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 tab of the Auth0 Dashboard's APIs section.
This example demonstrates:
Create a cross-origin verification page
Configure the Sample project
The sample code has an
appsettings.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.
Error Codes and Descriptions
Validate Access Tokens
Browser testing support
The seed project references the new ASP.NET Core metapackage (
Microsoft.AspNetCore.All), which includes all the NuGet packages that are a part of the ASP.NET Core 2.0 framework.
If you are not using the
Microsoft.AspNetCore.All metapackage, add the
Microsoft.AspNetCore.Authentication.JwtBearer package to your application.
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
AddAuthenticationmethod. Configure the JWT Bearer tokens as the default authentication and challenge schemes.
- Make a call to the
AddJwtBearermethod to register the JWT Bearer authentication scheme. Configure your Auth0 domain as the authority, and your Auth0 API identifier as the audience.
To add the authentication middleware to the middleware pipeline, add a call to the
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 called
HasScopeRequirement. 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.
ConfigureServices method, add a call to the
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
To secure an endpoint, you need to add the
[Authorize] attribute to your controller action:
To secure endpoints that require specific scopes, we need to make sure that the correct scope is present in the
access_token. To do that, add the
Authorize attribute to the
Scoped action, passing
read:messages as the