Securing Your Microservices: Azure B2C Authentication in ASP.NET Core API with Ocelot API Gateway
Introduction:
Microservices architecture offers flexibility and scalability but also presents challenges in managing authentication and authorization across multiple services. In this blog post, we will explore how to secure your microservices using Azure B2C authentication in ASP.NET Core API with Ocelot API Gateway. We’ll start by configuring Azure B2C for authentication and then integrate it with our ASP.NET Core API through Ocelot.
Prerequisites:
- Azure Subscription: You’ll need an Azure subscription to create and configure Azure B2C resources.
- Create one now if you haven’t already created your own Azure AD B2C Tenant. You can use an existing Azure AD B2C tenant.
- Visual Studio or Visual Studio Code: We’ll use Visual Studio or Visual Studio Code to create and run the ASP.NET Core API project.
- .NET Core SDK: Ensure that the .NET Core SDK is installed on your development machine.
- Azure CLI (Optional): Azure CLI provides a command-line interface for interacting with Azure resources. It’s optional but can help manage Azure resources.
Step 1: App registrations
- Sign in to the Azure portal (https://portal.azure.com) using your Azure account credentials.
- Navigate to the Azure Active Directory service and select App registrations.
- Click on “+ New registration” to create a new application registration.
- Provide a name for your application, select the appropriate account type, and specify the redirect URI for authentication callbacks.
- After creating the application registration, note down the Application (client) ID and Directory (tenant) ID.
Step 2: Create a client secret
- Once the application is registered, note the Application (client) ID and Directory (tenant) ID.
- If you are not on the application management screen, go to the Azure AD B2C—App registrations page and select the application you created.
- To access the Certificates & secrets settings, navigate to the Manage option and select it. The Certificates & secrets option can be found in the left menu.
- Under “Certificates & secrets”, generate a new client secret by clicking on New client secret.
- Enter a description of the client’s secret in the Description box. For example, Ocelotsecret.
- Under Expires, select a duration for which the secret is valid, and then click Add.
- Copy the secret’s Value for use in your client application code and save it securely.
Step 3: Configure scopes
- In the Azure AD B2C - App registrations page, select the application you created if you are not on the application management screen.
- Select App registrations. Select the OcelotTutorials application to open its Overview page.
- Under Manage, select Expose an API.
- Next to the Application ID URI, select the Add link.
- I have not changed the default GUID with my API, but you can replace the default value (a GUID) with an API and then select Save. The full URI is shown and should be in the format https://your-tenant-name.onmicrosoft.com/api. When your web application requests an access token for the API, it should add this URI as the prefix for each scope you define for the API.
-
Under Scopes defined by this API, select Add a scope.
-
Enter the following values to create a scope that defines read access to the API, then select Add scope:
Scope name: ocelottutorial.read
Admin consent display name: Read access to API Gateway API
Admin consent description: Allows read access to the API Gateway API
Step 4: Grant permissions
- Select App registrations and then the web application that should have access to the API, such as OcelotTutorials.
- Under Manage, select API permissions.
- Under Configured permissions, select Add a permission.
- Select the My APIs tab.
- Select the API to which the web application should be granted access. For example, webapi1.
- Under Permission, expand API Name, and then select the scopes that you defined earlier. For example, ocelottutorial.read and ocelottutorial.write.
- Select Add permissions.
- Select Grant admin consent for (your tenant name).
- If you’re prompted to select an account, select your currently signed-in administrator account, or sign in with an account in your Azure AD B2C tenant that’s been assigned at least the Cloud application administrator role.
- Select Yes.
- Select Refresh, and then verify that “Granted for …” appears under Status for both scopes.
Step 5: Enable ID token implicit grant
If you register this app and configure it with https://jwt.ms/ app for testing a user flow or custom policy, you need to enable the implicit grant flow in the app registration:
- In the left menu, under Manage, select Authentication.
- Under Implicit grant and hybrid flows, select both the Access tokens (used for implicit flows) and ID tokens (used for implicit and hybrid flows) checkboxes.
- Select Save.
Step 6: Set Up Azure B2C Authentication in ASP.NET Core API
-
Create 3 new ASP.NET Core Web API projects in Visual Studio or Visual Studio Code.
Accounting.API
Inventory.API
ApiGateway
- Assign the ports to the API. ApiGateay 9000, Accounting.API 9001, Inventory.API 9002
{ "Urls": "http://localhost:9001", "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*" }
-
Install the necessary NuGet packages for Azure B2C authentication. Install the below packages in the ApiGateway project
dotnet add package Microsoft.Identity.Web dotnet add package Ocelot
-
Configure Azure B2C authentication in your
Startup.cs
file:builder.Services.AddMicrosoftIdentityWebApiAuthentication(builder.Configuration);
-
Add the Azure B2C settings to your
appsettings.json
file:{ "Urls": "http://localhost:9000", "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "AzureAd": { "Instance": "https://login.microsoftonline.com/", "Domain": "http://localhost:9000/", "TenantId": "", "ClientId": "" } }
-
Ensure that the authentication middleware is added to the request processing pipeline in the
Configure
method ofStartup.cs
:app.UseAuthentication(); // Place UseAuthentication before UseOcelot app.UseAuthorization(); // Place UseAuthorization before UseAuthentication
- Add the
ocelot.json
file to the the ApiGateway with the below configuration{ "Routes": [ { "DownstreamPathTemplate": "/api/values", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 9001 } ], "UpstreamPathTemplate": "/accounting", "UpstreamHttpMethod": [ "GET" ], "AuthenticationOptions": { "AuthenticationProviderKey": "Bearer", "AllowedScopes": [] } }, { "DownstreamPathTemplate": "/api/values", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 9002 } ], "UpstreamPathTemplate": "/inventory", "UpstreamHttpMethod": [ "GET" ], "AuthenticationOptions": { "AuthenticationProviderKey": "Bearer", "AllowedScopes": [] } } ], "GlobalConfiguration": { "BaseUrl": "http://localhost:9000" } }
- Added ocelot configuration to the services
// Ocelot configuration builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); builder.Services.AddOcelot(builder.Configuration);
- Added Ocelot to the middleware pipeline in the end.
app.UseAuthentication(); // Place UseAuthentication before UseOcelot app.UseAuthorization(); // Place UseAuthorization before UseAuthentication app.MapControllers(); app.UseOcelot().Wait(); app.Run();
Step 7: Testing authentication To Test this refer to this tutorial OAuth 2.0 authorization code flow in Azure Active Directory B2C
- Replace the required fields and use the below URL in the browser to get the code to fetch the token. https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?client_id={client id}&response_type=code&response_mode=query&scope={scope uri}&state=007
- Open Postman and use the returned code to generate the token. See the image below to check the URL and the required fields to get the token.
https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token
- Now we are ready to call our API Gateway with the token.
Conclusion:
In this blog post, we’ve covered the first part of securing your microservices architecture using Azure B2C authentication. We walked through the process of configuring Azure B2C for authentication, including creating a tenant, setting up user flows (policies), and integrating Azure B2C authentication into an ASP.NET Core API project. In the next part of this series, we’ll explore how to integrate Azure B2C authentication with Ocelot API Gateway for centralized authentication and authorization management across microservices.
References:
Tutorial: Register a web application in Azure Active Directory B2C
Add a web API application to your Azure Active Directory B2C tenant
Tags: microservices,Ocelot,Azure AD B2C,dotnetcore