Docs

ASP.NET Core Web API v1.1: Authorization

View on Github

ASP.NET Core Web API v1.1: Authorization

Gravatar for andres.aguiar@auth0.com
By Andres Aguiar

This tutorial demonstrates how to add authorization to an ASP.NET Core 1.x 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 explore a sample app

2 minutes

Get a sample configured with your account settings or check it out on Github.

View on Github
System requirements: .NET Core SDK 1.1.0 | .NET Core 1.1.1 | Microsoft.AspNetCore.Authentication.JwtBearer 1.1.1 | Visual Studio 2017 or Visual Studio Code (Optional)

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. For Signing Algorithm, select RS256.

Create API

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.

We recommend using the default RS256 signing algorithm for your API. If you need to use the HS256 algorithm, see the HS256 integration sample.

Define scopes

Scopes let you define which resources can be accessed by 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 add the required scopes in the Scopes tab of the Auth0 Dashboard's APIs section.

Configure Scopes

This example uses the read:messages scope.

This example demonstrates:

  • 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, read the Verify Access Tokens tutorial.

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.

Validate Access Tokens

Install dependencies

To use Auth0 Access Tokens with ASP.NET Core you will use the JWT Middleware. Add the Microsoft.AspNetCore.Authentication.JwtBearer package to your application.

Configure the middleware

The ASP.NET Core JWT middleware will handle downloading the JSON Web Key Set (JWKS) file containing the public key for you, and will use that to verify the Access Token signature.

To add the JWT middleware to your application's middleware pipeline, go to the Configure method of your Startup class and add a call to UseJwtBearerAuthentication passing in the configured JwtBearerOptions. The JwtBearerOptions needs to specify your Auth0 API Identifier as the Audience, and the full path to your Auth0 domain as the Authority:

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 called HasScopeRequirement. This requirement will check that the scope claim issued by your Auth0 tenant is present, and if so it will ensure that the scope claim contains the requested scope. If it does then the Authorization Requirement is met.

Next, you can define a policy for each of the scopes in your application in the ConfigureServices method of your Startup class:

Protect API Endpoints

The JWT middleware integrates with the standard ASP.NET Core Authentication and Authorization mechanisms. To secure an endpoint you only need to decorate your controller action with the [Authorize] attribute:

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 policy parameter.

Use Auth0 for FREE