Azure AD B2C is Identity as a Service. It allows you to login into apps with a local account or use Google, Apple, or other providers.
Customers get to use their preferred accounts to sign up or create a regular user / pwd. You can customize the login experience.
App Types
This is best for:
- Server-based web apps work perfectly. They use OpenID Connect for all UX.
- Single page apps (SPA). Auth code flow with PKCE (implicit flow isn’t recommended).
- Mobile apps. They use OAuth2 auth code flow.
- Web services and APIs. OAuth2.
- Daemons. OAuth2 client credentials flow (still in public preview).
This doesn’t work with:
- Web API chains (on-behalf-of flow isn’t implemented).
Configure Azure Active Directory B2C
Flow:
- Create new resource
- Azure Active Directory B2C
- Create new tenant
- enter general information
Notice for the initial domain name, the full domain will be *.onmicrosoft.com. The location can’t be changed later.
Once a tenant is provisioned, click to manage it. You have to switch directory, this is because the B2C tenant is separate from your Azure subscription’s tenant. Your Azure subscription has a Microsoft Entra ID tenant associated with it and this new B2C one is distinct and separate. It’s its own directory.
Inside we have the following modules or tabs:
- App registrations: websites or web APIs or Apps
- Identity providers: lists all identity providers such as Google and your users may log in through them
- User attributes: you can see all metadata you collect from users
- Users: you can see and edit all users who have created accounts
- User flows: Heart and soul of Azure AD B2C as it guides users through the sign in or sign up, or password resets
- Identity Experience Framework: where you upload custom policies
- Audit Logs: Authentication of various apps or administrative events
Don’t modify or delete the b2c-extensions-app. It’s created automatically and B2C uses it to store user data.
Link Azure AD B2C Tenant
The B2C tenant has to be linked to an Azure subscription (for billing). Now this is done when you create the tenant, as you select the subscription and resource group there.
If you have an existing tenant which isn’t linked yet, you can still link it afterwards from the subscription.
Ways to provide identity UX
In Azure AD B2C you’ve two ways to provide identity UX:
- user flows - predefined, built-in, configurable policies so you can create sign-up/in and profile editing UX in minutes.
- custom policies - enable you to create your own user journeys for complex identity experience scenarios.
Both are reusable. Several apps can use the same one and one app can use several of them.
User flow
For the most common identity tasks. They control:
- Account types used for sign-in, such as social accounts or local accounts.
- Attributes to be collected from the user (you can collect standard or custom ones).
- To use MFA or not.
- User interface customization.
- Information inside the token that’s returned from B2C.
- Session behaviour.
Redirect URI - where to redirect response after the user’s been authenticated. As a test, put https://jwt.ms to see what’s inside the JWT token.
Built-in User Flows
There’re 3 recommended to have:
- Sign-in/up.
- Profile editing.
- Password reset.
There’re also separate Sign up and Sign in flows. Password reset can also be enabled inside the Sign-in/up flow (self-service password reset), so you don’t need a separate one. Use the Recommended version, the Standard (Legacy) ones aren’t updated anymore.
Social Providers
Social network provider are called identity providers (IdP). In order to use them, you have to configure them first. You register an app in the provider and get a client ID and a client secret which you enter into B2C. Then you enable the provider in the user flows where you want it and the authentication will happen seamlessly.
B2C Application Deep Dive
A B2C app models the real world. Every real-world app needs to be registered in B2C.
Redirect URI (reply URL): URL where responses will be returned to your webapp.
App ID (client ID): unique ID that identifies your B2C app.
They obey standards like OAuth 2.0 or OpenID Connect.
When you want to interact with it, you specify your user flow or custom policy.
Everything from Sign in to get Resource token happens inside Azure, not your app.
Tokens
They’re the way that B2C uses to transmit claims about a user calling Apps. ID and access tokens are JWT tokens.
ID token: contains claims used to identify user. (e.g. user’s object ID in AD). You must always validate it. Access token: Claims used to identify API permissions. Validate it. Refresh token: It should be a black box to your app (it’s opaque, only B2C can read it). It’s used to get new ones when the others expire. It can be invalidated anytime, so always save the new one you get.
By default ID and access tokens last 60 min and refresh tokens 14 days. You can change it in the user flow.
To validate a token, use a library. Check the signature (RS256, the kid in the header tells you which public key was used) and the claims aud, iss, nbf, exp and nonce. B2C rotates the keys, so check for new ones about every 24 hours.
Custom Policies
They’re config files that define the behaviour of your Azure AD B2C tenant UX. They can be fully edited by an identity developer to complete many different tasks.
It’s fully configurable and policy-driven.
- Federate with other identity providers
- Third party MFA
- Collect any user input
- Integrate with external systems using REST API communication
Each user journey is defined by a policy. You can build as many or as few policies as you need.
Defined by several XML files that refer to each other in a hierarchical chain.
Starter Pack
The starter pack comes with pre-built policies.
- LocalAccounts - Enables the use of local accounts only.
- SocialAccounts - Enables the use of social (or federated) accounts only.
- SocialAndLocalAccounts - Enables the use of both local and social accounts. Most of our samples refer to this policy.
- SocialAndLocalAccountsWithMFA - Enables social, local, and multi-factor authentication options.
Each starter pack includes:
- a base file that contains most definitions. To help with troubleshooting and long-term maintenance of your policies, try to minimize the number of changes you make to this file.
- a localization file holds the localization strings. This file is derived from the base file.
- an extension file holds the unique config changes for your tenant. This file is derived from the localization file. Use this to add new functionality or override existing functionality. e.g. to federate with new identity providers.
- a relying party (RP) file is the single task-focused file that’s invoked directly by the relying party application, such as web, mobile or desktop app. Each unique task such as sign-up/in, password reset etc requires its own relying party policy file. This file is derived from the extensions file. You can add more relying party policies. (e.g. delete my account, change a phone number…)
The basics
Claim
A claim provides temporary storage of data during an Azure AD B2C policy execution. It can store info about the user, such as first name, last name… or any other claim obtained from the user or other systems. The claim schema is the place where you declare your claims.
When a policy runs, B2C sends and receives claims to and from internal and external parties, then sends a subset of these claims to your relying party app as part of the token.
- Claims are saved, read, or updated against the directory user object.
- Claims are received from an external identity provider.
- Claims are sent or received using a custom REST API service.
- Data is collected as claims from the user during sign-up or edit profile flows.
Claims transformation are predefined functions to convert a given claim into another one, evaluate a claim or set a claim value.
Content definition
Things like how to render pages. It points to the URL of your HTML template.
Technical profile
They’re the interface to communicate with other parties, such as identity providers, REST APIs or the directory. They receive input claims and return output claims.
User Journey
They allow you to define business logic with path through which users will follow to gain access to your application. The user is taken through this journey to retrieve the claims that are to be presented to your app.
A user journey is built from a sequence of orchestration steps.
How you can add orchestration steps to social and local account starter pack.
Orchestration Steps
A user must reach the last step to acquire a token. Each step calls a technical profile. Orchestration steps can be conditionally executed based on preconditions. After an step completes, B2C stores the outputted claims in the claims bag. This bag can be used by any further orchestration steps in the user’s journey.
