ASP.NET Core Web API Authorization

Version: v2.0

Sample Project

Download a sample project specific to this tutorial configured with your Auth0 API Keys.

System Requirements
  • .NET Core 2.0
  • ASP.NET Core 2.0
  • Visual Studio 2017 (Optional)
  • Visual Studio Code (Optional)
Show requirements

This tutorial shows you how to use the authorization features in the OAuth 2.0 framework to limit access to your or third-party applications. For more information, read the API authorization documentation.

This tutorial shows you how to use access tokens from Auth0 to secure your ASP.NET Core Web API.

Before You Start

If you want to follow along with this tutorial, you can download the seed project. The seed project is a basic ASP.NET Web API with a simple controller and some NuGet packages. It also contains an appSettings.json file where you can configure the Auth0-related settings for your application.

To see what the project looks like after each of the steps, check the Quickstart folder of the Samples repository.

Create a Resource Server (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

Update the appsettings.json file in your project with the correct domain and API identifier for your API. See the example below:

{
  "Auth0": {
    "Domain": "YOUR_AUTH0_DOMAIN",
    "ApiIdentifier": "{YOUR_API_IDENTIFIER}"
  }
}

Add API Authorization

To restrict access to the resources served by your API, check the incoming requests for valid authorization information. The authorization information is stored in the access token created for the user and needs to be sent in the Authorization header. To see if the token is valid, check it against the JSON Web Key Set (JWKS) for your Auth0 account. To learn more about validating access tokens, read the Verify Access Tokens tutorial.

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 with the standard ASP.NET Core JWT middleware

Install Dependencies

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.

Install-Package Microsoft.AspNetCore.Authentication.JwtBearer

Configure the Middleware

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. You can access this public key 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.

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:

  1. Make a call to the AddAuthentication method. Configure the JWT Bearer tokens as the default authentication and challenge schemes.
  2. 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.
// Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // Some code omitted for brevity...

    string domain = $"https://{Configuration["Auth0:Domain"]}/";
    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

    }).AddJwtBearer(options =>
    {
        options.Authority = domain;
        options.Audience = Configuration["Auth0:ApiIdentifier"];
    });
}

To add the authentication middleware to the middleware pipeline, add a call to the UseAuthentication method:

// Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // Some code omitted for brevity...

    app.UseAuthentication();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

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:

// Controllers/ApiController.cs

[Route("api")]
public class ApiController : Controller
{
    [HttpGet]
    [Route("private")]
    [Authorize]
    public IActionResult Private()
    {
        return Json(new
        {
            Message = "Hello from a private endpoint! You need to be authenticated to see this."
        });
    }
}

Configure the Scopes

The JWT middleware shown above verifies if the user's access token included in the request is valid. The middleware doesn't check if the token has the sufficient scope to access the requested resources.

Scopes let you define which resources can be accessed by the user with a given access token. For example, you might choose to give the read access to the messages resource if a user has the manager access level, and a write access to that resource if they have the administrator access level.

To configure scopes, in your Auth0 dashboard, in the APIs section, click the Scopes tab. Configure the scopes you need.

Configure Scopes

This example uses the read:messages scope.

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.

// HasScopeRequirement.cs

public class HasScopeRequirement : IAuthorizationRequirement
{
    public string Issuer { get; }
    public string Scope { get; }

    public HasScopeRequirement(string scope, string issuer)
    {
        Scope = scope ?? throw new ArgumentNullException(nameof(scope));
        Issuer = issuer ?? throw new ArgumentNullException(nameof(issuer));
    }
}
// HasScopeHandler.cs

public class HasScopeHandler : AuthorizationHandler<HasScopeRequirement>
{
    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, HasScopeRequirement requirement)
    {
        // If user does not have the scope claim, get out of here
        if (!context.User.HasClaim(c => c.Type == "scope" && c.Issuer == requirement.Issuer))
            return Task.CompletedTask;

        // Split the scopes string into an array
        var scopes = context.User.FindFirst(c => c.Type == "scope" && c.Issuer == requirement.Issuer).Value.Split(' ');

        // Succeed if the scope array contains the required scope
        if (scopes.Any(s => s == requirement.Scope))
            context.Succeed(requirement);

        return Task.CompletedTask;
    }
}

In your 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:

// Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();

    string domain = $"https://{Configuration["Auth0:Domain"]}/";
    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

    }).AddJwtBearer(options =>
    {
        options.Authority = domain;
        options.Audience = Configuration["Auth0:ApiIdentifier"];
    });
    
    services.AddAuthorization(options =>
    {
        options.AddPolicy("read:messages", policy => policy.Requirements.Add(new HasScopeRequirement("read:messages", domain)));
    });

    // register the scope authorization handler
    services.AddSingleton<IAuthorizationHandler, HasScopeHandler>();
}

To secure the API endpoint, 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.

// Controllers/ApiController.cs

[Route("api")]
public class ApiController : Controller
{
    [HttpGet]
    [Route("private-scoped")]
    [Authorize("read:messages")]
    public IActionResult Scoped()
    {
        return Json(new
        {
            Message = "Hello from a private endpoint! You need to be authenticated and have a scope of read:messages to see this."
        });
    }
}
Next Tutorial
2. Using your API
Use Auth0 for FREECreate free Account