Intent.Security.MSAL
The Intent.Security.MSAL
module facilitates the integration of Azure Active Directory (Azure AD), EntraID, and Azure B2C to secure ASP.NET Core endpoints using the Microsoft.Identity.Web
NuGet package.
Prerequisites
- Azure subscription: You need an Azure subscription. If you don't have one, you can create a free account.
Note
If you were looking to make use of a different Open ID Connect Identity Provider solution, you will need to look at the Intent.Security.JWT
module instead.
Azure Entra ID / AD B2C Setup
Client Credential Setup
This setup is used when a back-end facing API (Client
) needs to invoke another downstream API (HostAPI
) with a secure credential where a user initiated the request (indirectly) or a system-driven event initiated it.
The following instructions assume a HostAPI
application exposing API endpoints and a Client
application consuming them. Adjust these settings based on your specific needs.
Registering the Host Application
- Sign in to the Azure portal.
- Navigate to Microsoft Entra ID > App registrations.
- Click New registration.
- Enter a Name for the application (e.g., "HostAPI").
- Select the Supported account types as
Accounts in this organizational directory only (Single tenant)
. - Leave the Redirect URI blank.
- Click Register to create the application.
Configure the Host Application
- Go to the application's Overview page.
- Under Manage, select Owners and add your Azure user as an owner.
- Select App roles and click on
Create app role
. - Provide it a Display name:
Client Access Role
. - Set the
Allowed member types
toApplication
. - Se the Value to
Client.Access
. - Give it a display name and description.
- Ensure that the
app role
is enabled by having the box checked at the end. - Click on
Apply
. - Select Expose an API and click on
Add
next toApplication ID URI
. - Keep the URI as-is (for tutorial purposes) and click
Save
.
Register the Client Application
- Navigate to Microsoft Entra ID > App registrations.
- Click New registration.
- Enter a Name for the application (e.g., "Client App").
- Select the Supported account types as
Accounts in this organizational directory only (Single tenant)
. - Leave the Redirect URI blank.
- Click Register to create the application.
Configure the Client Application
- After registering, go to the application's Overview page.
- Under Manage, select Certificates & Secrets and create a
Client secret
. COPY theValue
and store it somewhere as it will be obfuscated once you start navigating. - Navigate to
API permissions
. Click onAdd a permission
. - Go to
My APIs
. - Select
HostAPI
. - Select the
Client.Access
role while ensuring thatApplication permissions
are also set. - Click on
Add permissoins
. - Click on
Grant admin consent for Default Directory
.
Configure appsettings.json
Your API service hosting the endpoints for consumption needs to be configured using your Host Application
configuration from Microsoft Entra ID.
Ensure you specify the necessary properties for AzureAd
.
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "mydomain.onmicrosoft.com",
"TenantId": "19f43eb9-a915-45a5-9f1f-71091e8c8f1b",
"ClientId": "2825b206-549d-43c0-96c8-10a6a196d679",
"Audience": "api://2825b206-549d-43c0-96c8-10a6a196d679"
}
Domain
can be found inMicrosoft Entra ID
on itsOverview
page by copying thePrimary domain
.Instance
can be found by clicking onEndpoints
inApp registrations
. Copy thedomain part
from theOAuth 2.0 authorization endpoint (v2)
URI, i.e.,https://login.microsoftonline.com
.- Retrieve
ClientId
andTenantId
fromHostApi
'sOverview
page. Audience
is theApplication ID URI
from theHostApi
'sExpose an API
page.
Obtain an Access Token for Testing
For obtaining an Access Token, use tools like Postman, Insomnia or Bruno (they will have an Auth
section). Below is an example HTTP message to request an Access Token.
POST https://login.microsoftonline.com/19f43eb9-a915-45a5-9f1f-71091e8c8f1b/oauth2/v2.0/token
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials
&client_id=d348ba62-a064-476a-bac4-41af07d60d5a
&client_secret=Fay8Q~1MQUg4ZWcBM_5HEOQq9oJVvWbfmruJTaZy
&scope=api://2825b206-549d-43c0-96c8-10a6a196d679/.default
The URI can be found by accessing the Endpoints
page from HostApi
's Overview
page and copying the OAuth 2.0 token endpoint (v2)
URI.
The client_id
is your Client
application's Application Client ID and client_secret
will be the secret you copied earlier.
The scope
should be the Audience
from the HostAPI
having the suffix of /.default
.
Now you can test your token authorization by pasting the resulting access_token
into the Swagger UI under Authorize
(if you're using Swagger).
AD B2C Authorization Code Flow
This setup is used for requests initiated by a user from an SPA, Mobile app, or web page and processed by an externally exposed API (ConsumerAPI
).
Registering the ConsumerAPI Application
- Sign in to the Azure portal.
- Navigate to Azure AD B2C > App registrations.
- Click New registration.
- Enter a Name for the application (e.g., "ConsumerAPI").
- Select
Accounts in any identity provider or organizational directory (for authenticating users with user flows)
for Supported account types. - In
Redirect URI
, chooseWeb
and specify your application's URL and port, e.g.,http://localhost:8080
. - Ensure
Grant admin consent to openid and offline_access permissions
is checked. - Click Register to create the application.
Configuring the ConsumerAPI Application
- After registering, go to the application's Overview page.
- Under
Manage
, navigate toCertificates & secrets
and create aClient secret
. COPY theValue
and store it somewhere as it will be obfuscated once you start navigating. - Select
Expose an API
and click onAdd a scope
. Keep theApplication ID URI
as-is and click onSave and continue
(for tutorial purposes). - Provide a Scope name, e.g.,
FrontEnd.Access
. - Give it a display name and description.
- Click on
Add scope
. - In the
Owners
section, add your Azure user. - Navigate to
API permissions
, click onAdd a permission
. - Go to
My APIs
and selectConsumerAPI
. Select yourFrontEnd.Access
permission and click onAdd permissions
. - Click on
Grant admin consent for Default Directory
.
Setup User Flow
- Navigate to the Azure AD B2C instance.
- Locate
User flows
underPolicies
. - Click on
New user flow
. - Select
Sign up and sign in
and theRecommended
version. - Click
Create
. - Name the flow
B2C_1_Consumer
. - Check
Email signup
underLocal accounts
. - In the
User attributes and token claims
section, ensureDisplay name
is selected under attributes (for collected and returned scenarios). - Click
Create
.
Configure appsettings.json
Configure the settings for AzureAd
.
"AzureAd": {
"Instance": "https://myb2c.b2clogin.com",
"Domain": "myb2c.onmicrosoft.com",
"ClientId": "0feacea5-6401-45d0-9503-639bebf6ab73",
"SignUpSignInPolicyId": "B2C_1_Consumer"
}
Domain
can be found inAzure AD B2C
on itsOverview
page by copying thePrimary domain
.Instance
can be found by clicking onEndpoints
inApp registrations
. Copy thedomain part
from theOAuth 2.0 authorization endpoint (v2)
URI, i.e.,https://myb2c.b2clogin.com
.- Retrieve
ClientId
andTenantId
fromConsumerAPI
'sOverview
page. - Use the
B2C_1_Consumer
flow created earlier for theSignUpSignInPolicyId
setting.
Obtain an Access Token for Testing
For Authorization Code flow testing, use tools like Postman, Insomnia or Bruno (they will have an Auth
section) with the following configurations:
- Authorization URL:
OAuth 2.0 authorization endpoint (v2)
URL fromEndpoints
, replacing<policy-name>
withB2C_1_Consumer
. - Access Token URL:
OAuth 2.0 token endpoint (v2)
URL fromEndpoints
, replacing<policy-name>
withB2C_1_Consumer
. - Client ID:
Application (client) ID
. - Client Secret: Will be the secret you copied earlier.
- Callback URL:
Redirect URI
of theConsumerAPI
application. - Scope: Include
offline_access
,openid
, and your API scope (insideExpose an API
you can copy the created scope name), like:offline_access openid https://myb2c.onmicrosoft.com/0feacea5-6401-45d0-9503-639bebf6ab73/FrontEnd.Access
.
Click Get Access Token
, sign in, and copy the access_token
to paste into the Authorize
field in Swagger UI for testing.