top of page

How can you secure an ASP.NET Web API using OAuth and JWT?

ASP.NET Web API is a powerful platform for building RESTful APIs that can be consumed by a wide range of client applications. However, building a secure Web API is not always easy, especially when dealing with sensitive data and user authentication. One way to secure a Web API is to use OAuth and JWT. In this article, we will explore how to secure an ASP.NET Web API using OAuth and JWT.


What is OAuth?

OAuth is an open standard for authentication and authorization that allows users to grant access to third-party applications without sharing their credentials. With OAuth, a user can authenticate with a trusted service provider and obtain an access token that can be used to access protected resources on a third-party application.


OAuth 2.0 is the latest version of the OAuth protocol and provides several authorization flows that can be used to obtain access tokens. These flows include the Authorization Code Flow, Implicit Flow, Resource Owner Password Credentials Flow, and Client Credentials Flow.


To implement OAuth 2.0 in an ASP.NET Web API, you need to set up an OAuth server that can authenticate users and issue access tokens. This server can be implemented using an open-source OAuth server such as IdentityServer or a commercial solution such as Auth0.


Once you have set up an OAuth server, you need to configure your Web API to use OAuth for authentication. This involves configuring your API to validate access tokens and to allow only authorized users to access protected resources.


What is JWT?

JWT (JSON Web Token) is a standard for representing claims securely between two parties. A JWT consists of three parts: a header, a payload, and a signature. The header specifies the algorithm used to generate the signature, the payload contains the claims, and the signature is used to verify that the token has not been tampered with.


JWT can be used to implement authentication and authorization in an ASP.NET Web API. When a user authenticates with your API, a JWT token is created that contains the user's claims, such as their username, email address, and roles. This token is then passed with each API request to authenticate the user and authorize access to protected resources.


Securing an ASP.NET Web API using OAuth and JWT

To secure an ASP.NET Web API using OAuth and JWT, you need to follow these steps:


Step 1: Configure an Identity Provider (IdP)

The first step is to configure an Identity Provider (IdP) that supports OAuth and can issue JWT access tokens. There are many IdPs available, such as Azure Active Directory, IdentityServer, Auth0, Okta, etc. In this example, we will use IdentityServer as our IdP.


Step 2: Register your Web API as a Resource Server

The next step is to register your Web API as a Resource Server with the IdP and obtain a client ID and a client secret. These are credentials that identify your Web API to the IdP and allow it to request and validate access tokens.


Here is an example of how to configure IdentityServer to issue access tokens for your Web API:

services.AddIdentityServer()
    .AddInMemoryApiResources(new List<ApiResource>
    {
        new ApiResource("myapi", "My API")
    })
    .AddInMemoryClients(new List<Client>
    {
        new Client
        {
            ClientId = "myclient",
            ClientSecrets = { new Secret("myclientsecret".Sha256()) },
            AllowedGrantTypes = GrantTypes.ClientCredentials,
            AllowedScopes = { "myapi" }
        }
    });

In this example, we register an API resource called "myapi" and a client called "myclient". The client is configured to use the client credentials grant type and can access the "myapi" scope.


Step 3: Configure OAuth Middleware or Filters

The next step is to configure your Web API project to use OAuth middleware or filters that can handle incoming requests with access tokens. You can use built-in middleware or filters from ASP.NET Web API or third-party libraries such as Microsoft.Identity.Web or IdentityModel.AspNetCore.


Here is an example of how to use the built-in OAuth middleware in ASP.NET Core:

services.AddAuthentication("Bearer")
    .AddJwtBearer("Bearer", options =>
    {
        options.Authority = "https://localhost:5001";
        options.Audience = "myapi";
    });

In this example, we configure the built-in JwtBearer middleware to use the "Bearer" scheme and authenticate incoming requests with access tokens. We set the Authority to the URL of the IdentityServer instance and the Audience to the name of our API resource.


Step 4: Configure JWT Middleware or Filters

The next step is to configure your Web API project to use JWT middleware or filters that can validate the access tokens by checking their signature, expiration time, audience, scope, etc. You can use built-in middleware or filters from ASP.NET Web API or third-party libraries such as Microsoft.Identity.Web or IdentityModel.AspNetCore.


Here is an example of how to use the built-in JWT middleware in ASP.NET Core:

services.AddAuthorization(options =>
{
    options.AddPolicy("MyPolicy", policy =>
    {
        policy.RequireAuthenticatedUser();
        policy.RequireClaim("scope", "myapi");
    });
});

app.UseAuthentication();
app.UseAuthorization();

In this example, we configure an authorization policy called "MyPolicy" that requires an authenticated user and a claim with the "myapi" scope. We then add the policy to the AuthorizationOptions and use the UseAuthentication and UseAuthorization middleware to enable authentication and authorization in our Web API project.


Step 5: Configure Authorization Attributes or Policies

The next step is to configure your Web API controllers or actions to use authorization attributes or policies that can enforce role-based or claim-based authorization based on the access token's payload. You can use built-in attributes or policies from ASP.NET Web API or third-party libraries such as Microsoft.Identity.Web or IdentityModel.AspNetCore.


Here is an example of how to use the built-in Authorize attribute in ASP.NET Core:

[Authorize(Policy = "MyPolicy")]
public class MyController : ControllerBase
{
    // ...
}

In this example, we use the Authorize attribute to require the "MyPolicy" policy for all actions in the MyController class.


Step 6: Configure Web API Clients

The next step is to configure your Web API clients (such as mobile apps, web apps, etc.) to use OAuth libraries or SDKs that can obtain access tokens from the IdP using the appropriate flow (such as authorization code flow, implicit flow, client credentials flow, etc.). You can use built-in libraries or SDKs from ASP.NET Core or third-party libraries such as MSAL.NET or IdentityModel.


Here is an example of how to use the MSAL.NET library to obtain an access token for your Web API:

var app = PublicClientApplicationBuilder.Create("myclientid")
    .WithAuthority("https://login.microsoftonline.com/mytenantid")
    .WithRedirectUri("https://localhost:5002/redirect")
    .Build();

var result = await app.AcquireTokenForClient(new string[] { "myapi/.default" }).ExecuteAsync();
var accessToken = result.AccessToken;

In this example, we create a PublicClientApplication instance with the client ID and authority URL of our IdP. We then use the AcquireTokenForClient method to obtain an access token for the "myapi" resource using the client credentials flow.


Step 7: Send Access Tokens to Web API

The final step is to configure your Web API clients to send the access tokens to the Web API using the HTTP Authorization header with the Bearer scheme.


Here is an example of how to send an access token to a Web API using the HttpClient class in C#:

var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

var response = await client.GetAsync("https://localhost:5003/api/my");
var content = await response.Content.ReadAsStringAsync();

In this example, we create a HttpClient instance and set the Authorization header to the access token obtained in the previous step. We then send a GET request to the "/api/my" endpoint of our Web API and read the response content.


By following these steps, you can secure your ASP.NET Web API using OAuth and JWT and ensure that only authorized clients and users can access your data and services.


Conclusion

In this tutorial, we have seen how to secure an ASP.NET Web API using OAuth and JWT. We have configured an Identity Provider (IdP), registered our Web API as a Resource Server, and used OAuth middleware and JWT middleware to authenticate and authorize incoming requests. We have also seen how to configure authorization policies and use access tokens in Web API clients.

0 comments
bottom of page