What are we going to do?
This is the second article in the series on how to integrate a web application that is built using Angular and ASP.NET core web APIs, with Azure Active Directory. You can see all the parts below:
This is Part 2: Set up Asp.net core web APIs to use Azure AD Authentication. Here I will explain what code is required to integrate Azure AD with your Asp.Net Core Web API project.
- Must have followed what we covered in Part 1: Set up the Azure Active Directory and should have the client and tenant Id created in the previous article.
- Basic knowledge of Asp.Net Core Web APIs.
- Must have an asp-net core web API project setup.
All right, now we are good to go. Let's get started.
Let's make some code change sin the Asp.net Core Web APIs project.
Add the following in your appsettings.json file, and replace the Domain, TenentId, ClientId with the value you copied from Azure AD.
- "AzureAd": {
- "Instance": "https://login.microsoftonline.com/",
- "Domain": "replace with the domain name",
- "TenantId": "put azure ad tenant id",
- "ClientId": "put your api application's tenant id"
- }
You can easily find these values on the overview screen of the api application we registered in
part 1
Next let's create two class files in the root folder of the application and name it AzureAdOptions, AzureAdServiceCollectionExtensions and then paste the following code respectively.
- public class AzureAdOptions
- {
- public string ClientId { get; set; }
- public string ClientSecret { get; set; }
- public string Instance { get; set; }
- public string Domain { get; set; }
- public string TenantId { get; set; }
- }
- public static class AzureAdServiceCollectionExtensions
- {
- public static AuthenticationBuilder AddAzureAdBearer(this AuthenticationBuilder builder)
- => builder.AddAzureAdBearer(_ => { });
-
- public static AuthenticationBuilder AddAzureAdBearer(this AuthenticationBuilder builder, Action<AzureAdOptions> configureOptions)
- {
- builder.Services.Configure(configureOptions);
- builder.Services.AddSingleton<IConfigureOptions<JwtBearerOptions>, ConfigureAzureOptions>();
- builder.AddJwtBearer();
- return builder;
- }
-
- private class ConfigureAzureOptions : IConfigureNamedOptions<JwtBearerOptions>
- {
- private readonly AzureAdOptions _azureOptions;
-
- public ConfigureAzureOptions(IOptions<AzureAdOptions> azureOptions)
- {
- _azureOptions = azureOptions.Value;
- }
-
- public void Configure(string name, JwtBearerOptions options)
- {
- options.Audience = _azureOptions.ClientId;
- options.Authority = $"{_azureOptions.Instance}{_azureOptions.TenantId}";
- }
-
- public void Configure(JwtBearerOptions options)
- {
- Configure(Options.DefaultName, options);
- }
- }
- }
The last step is to use this code in startup.cs class. Paste the following code in ConfigureServices method.
- services.AddAuthentication(sharedOptions =>
- {
- sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
- }).AddAzureAdBearer(options => Configuration.Bind("AzureAd", options));
Make sure you import the class namespace before using this method.
That is it, we are done with API integration. Now it's time to make some code changes in our client application which is built on Angular 8. Let's do that
next.
Thanks for reading this article. Please feel free to share feedback or any question you have. You can find the code my public
git hub repo.