Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

A sample showing how to secure an ASP.NET Core web API with Azure AD B2C

License

Notifications You must be signed in to change notification settings

estiller/AzureB2CWithAspNetCoreSample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AzureB2CWithAspNetCoreSample

This sample shows how to secure an ASP.NET Core Web API using Azure AD B2C.

How To Run This Sample

To run this sample you will need:

  • Visual Studio 2015
  • ASP.NET Core SDK
  • An Internet connection
  • An Azure AD B2C tenant

Steps to follow:

  1. If you don't have an Azure AD B2C tenant, you can follow those instructions to create one.
  2. If you don't have a "Sign Up or Sign In" Policy, then create one as described here.
  3. Configure the Tenant, Client Id & Policy Id in the "TenantConfig.cs" file.

Key Takeaway

The key component for securing the API is the following code:

public void Configure(IApplicationBuilder app)
{
  var tokenValidationParameters = new TokenValidationParameters
  {
    // Configure the Web API to accept tokens requested only for it
    ValidAudience = TenantConfig.ClientId,
  };

  app.UseJwtBearerAuthentication(new JwtBearerOptions
  {
      // Configure the Discoery Document URL for configuring JWT settings
      MetadataAddress = $"https://login.microsoftonline.com/{TenantConfig.Tenant}/v2.0/.well-known/openid-configuration?p={TenantConfig.PolicyId}",
      TokenValidationParameters = tokenValidationParameters
  });

  app.UseMvc();
}  

About

A sample showing how to secure an ASP.NET Core web API with Azure AD B2C

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages