Make sure you have the following:
- A registered application in Azure AD B2C.
- A Policy defined (like a sign-up/sign-in policy).
- A Client ID (Application ID) from your B2C app.
- A Scope related to your Web API.
- Postman installed.
Ensure that you’ve configured your B2C App with the Implicit Grant Flow in the Azure AD B2C portal. Here's a quick check of what should be configured in Azure AD B2C:
-
Register the Web App (OAuth 2.0 Client):
-
Go to Azure Portal > Azure Active Directory > App Registrations.
-
Select your B2C app.
-
Under Authentication, configure:
- Platform: Web.
- Redirect URI: Enter your app's redirect URI (for testing purposes, this could be
https://oauth.pstmn.io/v1/browser-callbackor a predefined URI in your B2C app). - Implicit Grant: Enable ID Token and Access Token if needed.
-
Under API Permissions, configure the scope related to your Web API, e.g.,
https://{tenant}.onmicrosoft.com/{web api app id uri}/{scope name}. -
Note the Client ID and Tenant name, which you will use in Postman.
-
-
Scope: This will be the scope you are requesting when obtaining the access token. You can configure it in the Azure portal.
-
Launch Postman: Open Postman to configure the OAuth 2.0 flow.
-
Create a New Request:
-
Click New and select Request.
-
Name the request (e.g.,
OAuth 2.0 Implicit Flow). -
Enter the Auth URL in the URL field:
https://{tenant}.b2clogin.com/te/{tenant}.onmicrosoft.com/{policy}/oauth2/v2.0/authorize- Replace
{tenant}with your actual tenant name. - Replace
{policy}with the name of your B2C policy (e.g.,b2c_1_signup_signinorb2c_1a_signup_signin).
- Replace
-
-
Authorization Tab:
- Go to the Authorization tab in Postman.
- Set Type to
OAuth 2.0. - For OAuth 2.0 Flow, select Implicit.
-
Configure OAuth 2.0 Settings:
-
Client ID: Paste your Application (Client) ID from your B2C app.
-
Client Secret: Leave this empty for Implicit flow (not needed).
-
Auth URL: Enter the URL you obtained earlier:
https://{tenant}.b2clogin.com/te/{tenant}.onmicrosoft.com/{policy}/oauth2/v2.0/authorize -
Scope: Enter your desired scope:
https://{tenant}.onmicrosoft.com/{web api app id uri}/{scope name}- Replace
{tenant}with your actual tenant. - Replace
{web api app id uri}and{scope name}with the details of your API scope.
- Replace
-
Redirect URL: Enter the redirect URL defined in your B2C app settings, e.g.,
https://oauth.pstmn.io/v1/browser-callback(or any other valid redirect URI). -
State: Optional, but can be set to a random string for security purposes.
-
Response Type: Ensure this is set to
id_token token(this is the standard response for Implicit flow).
Example configuration:
- Auth URL:
https://contoso.b2clogin.com/te/contoso.onmicrosoft.com/b2c_1_signup_signin/oauth2/v2.0/authorize - Client ID:
12345678-abcd-1234-abcd-1234567890ab - Scope:
https://contoso.onmicrosoft.com/my-api/scope.read - Redirect URI:
https://oauth.pstmn.io/v1/browser-callback
-
-
Get New Access Token:
- Once everything is configured, click on the Get New Access Token button in Postman.
- This will redirect you to the B2C login page (depending on the policy you configured, it may prompt for sign-in or show a custom UI).
- Log in with your credentials.
- Postman should automatically capture the Access Token (and optionally an ID Token) after successful authentication.
-
Use the Token:
- After you obtain the token, Postman will display the Access Token in a dialog.
- You can then Use Token for subsequent requests.
After obtaining the Access Token, you can use it to make API calls that require authentication. Here’s how to do that:
-
Set Authorization Header:
-
In the Headers section of Postman, add the following:
- Key:
Authorization - Value:
Bearer {access_token}
- Key:
-
Replace
{access_token}with the actual token you received from the Get New Access Token step.
-
-
Make the Request:
-
Set the request URL to the endpoint of the API you want to call.
-
For example:
https://{api_url}/your-api-endpoint
-
-
Send the Request:
- Click Send to make the request. If everything is set up correctly, you should receive a valid response from the API.
Here are a few things to check if the process isn't working as expected:
- Redirect URI: Make sure that the redirect URI in Postman matches the one you set in Azure AD B2C. It’s case-sensitive.
- Scope: Verify that the scope is correctly defined in both your B2C app and Postman.
- Policy: Ensure the policy (e.g.,
b2c_1_signup_signinorb2c_1a_signup_signin) is correctly specified in the Auth URL. - Browser Popup: Sometimes the authentication page may not load in Postman directly, but it will open in a browser. Ensure that the browser allows pop-ups.
- Security: Implicit flow is typically used for client-side applications (e.g., Single Page Apps). Since it exposes the access token to the browser, be cautious about using this in production environments for highly sensitive applications.
- Access Token Expiry: Tokens obtained using Implicit flow usually have a short expiration time. Be prepared to handle token expiry and refresh tokens (although Implicit flow does not use refresh tokens, so the user would need to authenticate again).