There are a large number of OAuth systems you can connect to for authentication of your website users.
Here are some insights on how to connect to Azure B2C. It will show you how to configure it and in part 2 we will cover some custom policies using B2C.
By Danny Paul van Iersel, 04-6-2024
For the connection with B2C on Sitecore Authentication we use OpenIdConnect.
Important notice when using OpenIdConnect and using the TokenClient to read the token we receive. We use the IdentityModel package, the version is crucial because Sitecore uses this as well. For Sitecore the Identity Server from the Sitecore Back-End login uses version 3.6.1.
This must match with our implementation, otherwise we will receive dependency issues when trying to login to Sitecore itself.
Parts we need to make the B2C connection work:
Within your Azure Subscription, select “Create a resource”. Search for B2C.
Create an Azure Active Directory B2C.
We have the option to either:
For this demo we created a new Tenant.
Once we have created a new Tenant we will find this Tenant under our resources.
On the overview you will find the option to open the B2C Tenant in the middle of the screen.
This will redirect you to the new Tenant.
It sometimes might be confusing that all of your resources appear to have disappeared. This is because we are on a different directory now. This directory is specially dedicated to the B2C Tenant.
When developing and changing from 1 directory to the other it can cause you to lose some directions. Keep in mind to check your directory at the top of your screen underneath your profile name (or email).
From the B2C we can configure a default user flow. This is the easiest way to test our connection and make sure our redirects and callbacks work correctly.
Go to the user flows and choose “New user flow”. We find it easy to use the sing up and sign up in flow because this already contains several functions we will be needing later on.
You can then configure the policy.
Once we have our flow configured we need to register our own application. This makes it possible for our application to connect with our B2C.
Add a new registration:
Now we will configure our code. First let us look at the configuration file that needs to be patched.
Here we have the following sections configured:
Next we need to configure our Provider Processor.
In our Provider Processor we configure the settings for the Identity provider and handle the options to redirect and callbacks.
Here you see the basic setup for our Identity Provider Processor.
In the ProcessCore method we will add the following:
The variables like ClientID and Secret should be stored in any configuration file or site settings within the Sitecore environment.
The maximum age of the Client Secret is 24 months, keep in mind that this variable needs to be adjustable.
We create an new set of options for the OpenIdConnectAuthenticationOptions.
The MetadataAddress contains the configuration of the B2C URL’s. It will guide the user to the login screen designed for the policy we have configured.
With Notifications we set the callbacks once the user has logged in. If the authentication has failed or if we receive a authentication code.
In the end we need to register our application to use the authentication. With args.App.UseOpenIdConnectAuthentication(openIdAuthOptions); we set the application to use this configuration for our identity provider.
We have setup the provider and configured the following settings to be used:
We can now setup our OpenIdConnectAuthenticationOptions. The options we set are:
The final part is to register our application to use the OpenIdConnect:
args.App.UseOpenIdConnectAuthentication(openIdAuthOptions);
After setting up our provider we need a page to show the login methods.
var args = new GetSignInUrlInfoArgs("website", url);
args.Result.ToList();
By using this we can loop through our login possibilities. It is great to work with and very easy to customize.
To know how to configure the B2C custom policies you can read our second part: Sitecore authentication with Azure B2C Custom policies Part 2
Are you having troubles with your setup for authentications or need assistance? Contact us.
We are happy to help you.
Ready to take your digital experience to the next level? Feel free to contact us to learn more about our services and how we can help you leverage the full potential of your digital marketing.
Get in touch with one of our consultants to find the perfect match that fits your needs and enables you to grow.