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.



Step 3: Create User Flow

We have created application now we will create userFlow. Just to give brief idea about userFlow So,

User flows are predefined, built-in, configurable policies that azure provide so we can create sign-up, sign-in, and policy editing experiences in minutes.

we will be looking into "UserFlow" in new blog. Right now we will be just creating 1 userFlow for signup and signIn.

[1] SignUpSignIn
    
First select "userFlow" from menu and as below image.

You will see below screen. You can see that I have already created 2 flows here. Now click on "New User Flow".

Once you click on "New User Flow" you will be redirected to the below screen. Here We have to select "Sign Up and Sign In" option.
Once we click on the option on the same page we can see the "Create" button.

Once we click on create we will be redirect to the next page. Here we have to fill the form. Please go throw below image and do the same.
In the "User attributes and token claims" option click on "Show More" and you see one panel.
Select high lighted options in the panel. and create the flow.
Once we create the flow we can see it in the list.



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.


It will open new screen with different endpoints. We need to take url with "b2clogin.com"  as high lighted in the image. It will be our tenant name.


For getting client name again click on "App Registration" you will get application list. Here you will see the applicationId with application name. Select applicationId that you have created.




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. 


Here we need to add url of out .net core application. Once you add the url scroll it little bit and you will see below configuration


check the checkboxes and save the configuration. Now we are ready to test our application.

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. .



You should see a sign-up and sign-in page by clicking on signIn link.


Here click on "Sign up now" to register new user.


Once we create new user we will be redirected to the home page of our application. We can see the name of logged in user at top right corner.

Now if we want to see where the user is created then go to the azure portal and select "User" option.


After selection of  "User" option we can see the list of users.


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.

I hope this blog will be usefull to get better idea about azure b2c. I will be back with new blog where we will look into how we can use third party like Gmail, Facebook, Amazon to login into our application with Azure AD B2C.

Comments

Popular posts from this blog

The Ultimate Guide to Userflow in Azure AD B2C: Boosting User Experience and Security

Next-Level Identity Management: How to Integrate Azure AD B2C with Google Accounts - Part2