Azure

Setup ASP.NET Core Net APIs To Use Azure AD Authentication

What are we going to do?

 

That is the second article within the sequence on the right way to combine an online utility that’s constructed utilizing Angular and ASP.NET core internet APIs, with Azure Lively Listing. You may see all of the elements under:

That is Half 2: Arrange Asp.web core internet APIs to make use of Azure AD Authentication. Right here I’ll clarify what code is required to combine Azure AD along with your Asp.Web Core Net API challenge. 

 

  • Should have adopted what we coated in Half 1: Arrange the Azure Lively Listing and will have the shopper and tenant Id created within the earlier article. 
  • Fundamental information of Asp.Web Core Net APIs.
  • Should have an asp-net core internet API challenge setup.

All proper, now we’re good to go. Let’s get began.

 

Let’s make some code change sin the Asp.web Core Net APIs challenge.

 

Add the next in your appsettings.json file, and change the Area, TenentId, ClientId with the worth you copied from Azure AD.

  1. “AzureAd”: {  
  2.     “Occasion”“https://login.microsoftonline.com/”,  
  3.     “Area”“change with the area title”  
  4.     “TenantId”“put azure advert tenant id”,  
  5.     “ClientId”“put your api utility’s tenant id”  
  6.   }  

You may simply discover these values on the overview display screen of the api utility we registered in half 1

 

 

Subsequent let’s create two class recordsdata within the root folder of the appliance and title it AzureAdOptions, AzureAdServiceCollectionExtensions after which paste the next code respectively. 

  1. public class AzureAdOptions  
  2.    {  
  3.        public string ClientId { getset; }  
  4.        public string ClientSecret { getset; }  
  5.        public string Occasion { getset; }  
  6.        public string Area { getset; }  
  7.        public string TenantId { getset; }  
  8.    }  
  1. public static class AzureAdServiceCollectionExtensions  
  2.     {  
  3.         public static AuthenticationBuilder AddAzureAdBearer(this AuthenticationBuilder builder)  
  4.                    => builder.AddAzureAdBearer(_ => { });  
  5.   
  6.         public static AuthenticationBuilder AddAzureAdBearer(this AuthenticationBuilder builder, Motion<AzureAdOptions> configureOptions)  
  7.         {  
  8.             builder.Providers.Configure(configureOptions);  
  9.             builder.Providers.AddSingleton<IConfigureOptions<JwtBearerOptions>, ConfigureAzureOptions>();  
  10.             builder.AddJwtBearer();  
  11.             return builder;  
  12.         }  
  13.   
  14.         non-public class ConfigureAzureOptions : IConfigureNamedOptions<JwtBearerOptions>  
  15.         {  
  16.             non-public readonly AzureAdOptions _azureOptions;  
  17.   
  18.             public ConfigureAzureOptions(IOptions<AzureAdOptions> azureOptions)  
  19.             {  
  20.                 _azureOptions = azureOptions.Worth;  
  21.             }  
  22.   
  23.             public void Configure(string title, JwtBearerOptions choices)  
  24.             {  
  25.                 choices.Viewers = _azureOptions.ClientId;  
  26.                 choices.Authority = $“{_azureOptions.Occasion}{_azureOptions.TenantId}”;  
  27.             }  
  28.   
  29.             public void Configure(JwtBearerOptions choices)  
  30.             {  
  31.                 Configure(Choices.DefaultName, choices);  
  32.             }  
  33.         }  
  34.     }  

The final step is to make use of this code in startup.cs class. Paste the next code in ConfigureServices methodology. 

  1. companies.AddAuthentication(sharedOptions =>  
  2.            {  
  3.                sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;  
  4.            }).AddAzureAdBearer(choices => Configuration.Bind(“AzureAd”, choices));  

Be sure to import the category namespace earlier than utilizing this methodology. 

 

That’s it, we’re completed with API integration. Now it is time to make some code adjustments in our shopper utility which is constructed on Angular 8. Let’s do this subsequent.

 

Thanks for studying this text. Please be at liberty to share suggestions or any query you have got.  You will discover the code my public git hub repo.

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button