Azure B2C
Azure B2C Integration with OwnID
By integrating Azure B2C with OwnID, you can implement the full set of OwnID features to streamline your user authentication experience.
Prerequisites
- Azure B2C tenant
- Azure application with Microsoft Graph API permissions
How it Works
Integrate OwnID with Azure B2C by completing these four basic steps:
- Step 1 - Configure your Azure B2C tenant.
- Step 2 - Build 3 server endpoints
- Step 3 - Create an OwnID application in the OwnID Console.
- Step 4 - Integrate with your frontend.
Step 1 - Configure Azure B2C
Use Your Existing Azure B2C Tenant
If you already have an Azure B2C tenant set up:
- Sign in to the Azure Portal
- Switch to your Azure B2C tenant by clicking on your account in the top right corner
- Select your B2C directory from the dropdown
Note: If you don’t have an existing B2C tenant, you can create one by:
- Navigating to
Create a resource > Identity > Azure Active Directory B2C
- Selecting
Create a new Azure AD B2C Tenant
- Filling in the required information (Organization name, Domain name, Country/Region, etc.)
- Clicking
Review + create
and thenCreate
Register an Application for OwnID Integration
You’ll need to register a specific application for OwnID integration, even if you have other applications already registered in your tenant:
- In your B2C tenant, navigate to
App registrations
- Click
New registration
- Fill in the required information:
- Name: OwnID Integration (or any name you prefer)
- Supported account types: Accounts in this organizational directory only (default directory only - single tenant)
- Click
Register
- After registration, note down the following values:
- Application (client) ID
- Directory (tenant) ID
- Object ID
Create Client Secret
- In your registered app, navigate to
Certificates & secrets
- Click
New client secret
- Add a description and select expiration period
- Click
Add
- Important: Note down the client secret value as it will be shown only once
Configure API Permissions
- In your registered app, navigate to
API permissions
- Click
Add a permission
- Select
Microsoft Graph
- Select
Application permissions
- Add the following permissions:
- User.ReadWrite.All (to manage users)
- Directory.ReadWrite.All (to manage directory)
- Click
Add permissions
- Important: Click
Grant admin consent for [your-tenant-name]
button at the top of the API permissions page. This step is critical - without admin consent, you will get “Insufficient privileges” errors when accessing the Graph API.
Find Your B2C Extension App ID
Every Azure B2C tenant has a special application called the b2c-extensions-app
that’s automatically created:
- In your B2C tenant, go to
App registrations
- Switch to
All applications
if you don’t see it immediately - Look for
b2c-extensions-app. Do not modify. Used by AADB2C for storing user data.
- Note down its Application (client) ID - you’ll need this for your environment variables
This Extension App ID is crucial for working with custom attributes:
- Your existing B2C setup likely already uses this app for custom user attributes
- The OwnID integration will store its data as custom attributes using this same app
- No manual attribute creation is needed - attributes are created automatically when first used
- Custom attributes in B2C follow a specific naming convention that our code handles automatically
Configure User Flow (Policy)
You need to use or create an appropriate user flow (policy) in your Azure B2C tenant that includes the necessary claims:
-
Go to your Azure AD B2C tenant in the Azure Portal
-
In the left menu, select
User flows
-
Either use an existing user flow or create a new one:
- To create a new flow, click
+ New user flow
- Select
Sign in
as the type (orSign up and sign in
) - Choose
Recommended
version - Enter a name (e.g.,
signin
) - it will be prefixed withB2C_1_
- To create a new flow, click
-
Important: Configure the correct return claims
- In your user flow, go to
Application claims
- Ensure these claims are selected as
Return claims
:- Email Addresses (required for user identification)
- User’s Object ID (required for user operations)
- Display Name (recommended)
- In your user flow, go to
-
Save your user flow configuration
-
Note the full name of your user flow (e.g.,
B2C_1_signin
)
Step 2 - Build 3 Server Endpoints
In this step, you need to expose the following API endpoints for OwnID to use in server-to-server calls:
Set OwnID Data for a User
Endpoint: POST /setOwnIDDataByLoginId
Description: Stores OwnID authentication data (account.ownIdData) for an existing user in Azure B2C.
Request Body from OwnID:
Endpoint implementation example:
Expected Responses:
Status | Description | Return |
---|---|---|
204 | Success | no content returned |
404 | User not found | no content returned |
Get OwnID Data for a User
Endpoint: POST /getOwnIDDataByLoginId
Description: Retrieves OwnID authentication data (account.ownIdData) for an existing user in Azure B2C.
Request Body from OwnID:
Endpoint Implementation Example:
Expected Responses:
Status | Description | Return |
---|---|---|
200 | User found and has ownIdData | return ownIdData String |
204 | User found but doesn’t have ownIdData | no content returned |
404 | User not found | no content returned |
Generate Session for a User
Endpoint: POST /getSessionByLoginId
Description: Generates Azure B2C authentication tokens for an existing user.
Request Body:
Endpoint Implementation Example:
Expected Responses:
Status | Description | Return |
---|---|---|
200 | Success | Azure B2C tokens |
404 | User not found | no content returned |
Authentication:
This endpoint acquires standard tokens from Azure B2C that can be used with Microsoft’s authentication libraries. The frontend application should use MSAL.js to handle these tokens for proper authentication flow.
Secure Your Endpoints
Requests from the OwnID server include two headers that can be used to ensure the request has not been tampered with. The first one, ownid-signature
, is a hash value that the OwnID server generates from a timestamp and the body of the request. The second one, ownid-timestamp
can be used by your backend to calculate the signature that is based on the timestamp and request body, and then compare the result to the value of ownid-signature
. If both signatures do not match, the request has been altered.
Because the signatures are generated using an HMAC with the SHA256 hash function, the OwnID server and your backend must use the same cryptographic key when calculating the hash value. You can obtain this key from the OwnID Console, and then add the code generates a hash and compares it to the signature in your backend.
Obtaining the HMAC Key
Before the backend can generate the HMACSHA256 value, you must obtain the secret cryptographic key used in the calculation. Simply open your OwnID application in the OwnID Console and copy the value from MyApp > Shared Secret.
Request Verification
Now that you have the cryptographic key, the backend can verify requests by generating each request’s expected signature and compare it to the one generated by the OwnID server. The backend code must:
-
Step 1: Extract the
ownid-signature
andownid-timestamp
headers from the request. These headers provide the HMAC code generated by the OwnID server and the timestamp it used to generate it. -
Step 2: Validate the
ownid-timestamp
for expiration. To prevent replay attacks, check whether the provided timestamp is within an acceptable time window. Define a preferred expiration time, such as 1 minute, and validate against the current time. Requests with a timestamp older than this period should be rejected. -
Step 3: Create the data string that will be used as an input to the hash function. To create it you need to concatenate:
- The request body (in a JSON string format)
- The character
.
- The timestamp (from the
ownid-timestamp
header)
-
Step 4: Use HMAC with SHA256 to calculate a hashed value from the body-timestamp data string. The cryptographic key used in the calculation is the shared secret for your OwnID application.
-
Step 5: Compare the hash value generated by your backend with the signature extracted from the
ownid-signature
header.
The following code snippets show how the backend might accomplish these steps:
Complete Code Sample
Using ngrok for Development
During development, you can use ngrok to expose your local server to the internet, allowing OwnID services to communicate with your application:
Start your server with ngrok tunnel
Step 3: Create OwnID Application
An OwnID application connects your backend with the OwnID widget in the front end. This OwnID application is assigned a unique appId
that is then added to the website’s front end. To create an OwnID application:
- Open the OwnID Console and create an account or log in to an existing account.
- Select
Create Application
. - Define the name of your application, your backend language, and finish the onboarding.
Step 4 - Integrate with your Frontend
Choose your frontend integration path:
OwnID Elite
Implement the predefined authentication screens provided by OwnID
OwnID Boost
Add OwnID as an add-on to your existing forms
Next Steps
Ready to deploy?
YES!
Take me to the Deployment Checklist