Integrate Azure AD B2C With .Net Core Application
Hello Readers,
Welcome to my second blog on "Azure AD B2C" series. In the first part we have seen how to create Azure AD B2C Tenant step by step. If you haven't checked it yet then you can check it here.
Today we will look into how we can integrate B2C tenant in our .net core application. We will also see what are the benefits of using B2C.
Azure AD B2C is a cloud-based identity management service that enables you to secure and manage the identity of your customers and employees. The Azure AD B2C local account provider allows you to use a local account store instead of an external identity provider. In this blog, we will explore how to use the Azure AD B2C local account provider with a .NET Core application.
Step 1: Create an Azure AD B2C tenant
To get started, you need to have an Azure AD B2C tenant. If you do not already have one, you can check my first blog here where I have explained every thing in detail.
Step 2: Create an application in Azure AD B2C
Once you have created the tenant, you need to create an application in Azure AD B2C that represents your .NET Core application. To do this, navigate to the Azure portal and select the Azure AD B2C tenant you created in step 1. Click on "Applications" and then click on "Add". Select "Web application and/or Web API" and fill in the required details for your application as shown in below steps.
Select high lighted options in the panel. and create the flow.
Step 4: Create And Configure the .NET Core application
Use below command to create new .net core mvc application
dotnet new mvc -o AzureAdB2cDemoe -au IndividualB2C
Now open "appsettings.json" file and add below.
{
"AzureAdB2C": {
"Instance": "https://your-tenant-name.b2clogin.com/",
"ClientId": "your-client-id",
"Domain": "your-tenant-name.onmicrosoft.com",
"CallbackPath": "/signin-oidc",
"SignUpSignInPolicyId": "your-SignUpSignInPolicy",
}
}
Replace "your-tenant-name" with the name of your Azure AD B2C tenant, "your-client-id" with the client ID (ApplicationId) of your application, and your-SignUpSignInPolicyId.
To get tenant name click on the "App Registration" and then click on "EndPoints" as high lighted in the below image.
and in "your-SignUpSignInPolicyId" add name of the userFlow that we have created in step-3.
Now open "Program.cs" file and you will see authentication code is already added here.
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAdB2C"));
Now we have completed all the set up here only one thing is remaining that we need to connect our .net core application with the application that we have created in azure portal.
Let's move back to the Azure portal and select "App Registration" and select application that we have created in step2 and select "Authentication" option in that. We will see below screen.
Step 5: Test the implementation
Now that we have completed all of the steps, we can test the implementation by running our .NET Core application. .
Here click on "Sign up now" to register new user.
In conclusion, the Azure AD B2C local account provider allows you to secure and manage the identity of your customers and employees with a local account store instead of an external identity provider. By following the steps outlined in this blog, you can easily add the local account provider to your .NET Core application.
Comments
Post a Comment