Working with Azure B2C Single Sign On in Asp.net core website
When we want to use Azure B2C on our website for authentication first we need to create a tenant for that. It is a unique url which identifies your app and your login process will be redirected to that tenant url. For example, if we don't user Azure B2C, when we click on login button generally we see user name and password fields on the page. After entering the user name and password will be validated against our database. If they are correct then we allow them to access other pages on the website. But when use Azure B2C, the moment user clicks on login button they will be redirected to a page where the entered credentials will be validated against azure active directory. Then link for login button will be tenant url. To add Azure B2C authentication to your web site follow the below steps.
Step #1: Login to azure portal and create a Tenant.
Step#2: After creating tenant we need to create an application. This application comes with a key. By using this we can connect to the azure B2C. We need to provide return url. So that Azure B2C will know to which url they need to redirect once user is authenticated.
Step#3: We need to create policy. This policy tells which actions we want to perform like signin , signup and profile edit..etc.
Step #4: Now in Startup.cs file in the ConfigurationService method we need to configure.
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect("B2C_1A_SignInLocal",
options => SetOptionsForOpenIdConnectPolicy("B2C_1A_SignInLocal", options))
.AddCookie(options => { options.LoginPath = "/account/login"; });
}
private void SetOptionsForOpenIdConnectPolicy(string policy, OpenIdConnectOptions options)
{
options.MetadataAddress =
$"{Configuration["AzureB2CMetadataAddress"]}{policy}";
options.ClientId = $"{Configuration["AzureB2CCLientId"]}";
options.ResponseType = OpenIdConnectResponseType.IdToken;
options.CallbackPath = $"/signin/{policy}";
options.SignedOutCallbackPath = $"/signout/{policy}";
options.SignedOutRedirectUri = "/";
options.TokenValidationParameters.NameClaimType = "name";
}
In the above code we have configured for Signin policy.
For metadata address the pattern will be as follows.
https://login.microsoftonline.com/{tenant}/v2.0/.well-known/openid-configuration?p={plocy name}
Step #5: In the Login method of a controller challenge the policy. So that when user clicks on login it will automatically redirect to sign in page of Azure B2C.
[AllowAnonymous]
public IActionResult SignInB2c()
{
return Challenge(new AuthenticationProperties { RedirectUri = "/"}, "B2C_1A_SignInLocal");
}
Step #1: Login to azure portal and create a Tenant.
Step#2: After creating tenant we need to create an application. This application comes with a key. By using this we can connect to the azure B2C. We need to provide return url. So that Azure B2C will know to which url they need to redirect once user is authenticated.
Step#3: We need to create policy. This policy tells which actions we want to perform like signin , signup and profile edit..etc.
Step #4: Now in Startup.cs file in the ConfigurationService method we need to configure.
In the above code we have configured for Signin policy.
For metadata address the pattern will be as follows.
https://login.microsoftonline.com/{tenant}/v2.0/.well-known/openid-configuration?p={plocy name}
Step #5: In the Login method of a controller challenge the policy. So that when user clicks on login it will automatically redirect to sign in page of Azure B2C.
Comments
Post a Comment