This is a demonstration of how to use a Blazor 8 Web App to use Entra ID authentication and call a secured .NET Api.
Clicking "Claims" will display the Claims Principle information in the Blazor app.
Clicking "API" will call the api and pass the Entra ID to a endpoint secured with an [Authorize] attribute. The very that the credentials are flowing to the API, the endpoint will return the Claims Principal to the Blazor app.
- Clone this repo.
- Follow the instructions below to register the api and the client in Azure App Registrations.
- Update the appsettingdevelopment.json files for the client and api with the configurations as descriped in the app registrations section. Make sure not to let these files get into your repo because they will contain secrets.
- Make sure the Blazor client is configured to call the same port that the api is running on (the api port can be set in properties\launchSettings.json).
- Set the project properties to launch both api and client apps.
-
Navigate to the Azure portal.
-
Select the App Registrations blade on the left, then select New registration.
-
In the Register an application page that appears, enter your application's registration information:
- In the Name section, enter a meaningful application name that will be displayed to users of the app:
BlazorAuthDemo.Api
- Under Supported account types, select Accounts in this organizational directory only
- Select Register to create the application.
- In the Name section, enter a meaningful application name that will be displayed to users of the app:
-
In the Overview blade, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
-
In the app's registration screen, select the Expose an API blade to the left to open the page where you can publish the permission as an API for which client applications can obtain access tokens for. The first thing that we need to do is to declare the unique resource URI that the clients will be using to obtain access tokens for this API. To declare an resource URI(Application ID URI), follow the following steps:
- Select Add next to the Application ID URI to generate a URI that is unique for this app.
- For this sample, accept the proposed Application ID URI (
api://{Id}
) by selecting Save. Read more about Application ID URI at Validation differences by supported account types (signInAudience).
-
All APIs must publish a minimum of one scope, also called Delegated Permission, for the client apps to obtain an access token for a user successfully. To publish a scope, follow these steps:
-
Select Add a scope button open the Add a scope screen and Enter the values as indicated below:
- For Scope name, use BlazorAuthDemo.Read.
- Select Admins and users options for Who can consent?.
- For Admin consent display name type in
Read BlazorAuthDemo.Api
. - For Admin consent description type in
Allow the app to read BlazorAuthDemo.Api
. - For User consent display name type in
Read via the BlazorAuthDemo.Api
. - For User consent description type in
Allow the app to read via the BlazorAuthDemo.Api
. - Keep State as Enabled.
- Select the Add scope button on the bottom to save this scope.
- Repeat the steps above for another scope named BlazorAuthDemo.ReadWrite
-
Select the Manifest blade on the left.
- Set accessTokenAcceptedVersion property to 2.
- Select on Save.
-
All APIs should publish a minimum of one App role for applications, also called Application Permission, for the client apps to obtain an access token as themselves, i.e. when they are not signing-in a user. Application permissions are the type of permissions that APIs should publish when they want to enable client applications to successfully authenticate as themselves and not need to sign-in users. To publish an application permission, follow these steps:
-
Still on the same app registration, select the App roles blade to the left.
-
Select Create app role:
- For Display name, enter
BlazorAuthDemo.Read.All
. - For Allowed member types, choose Application to ensure other applications can be granted this permission.
- For Value, enter
BlazorAuthDemo.Read.All
. - For Description, enter
Allow the app to read using the BlazorAuthDemo.Api
. - Select Apply to save your changes.
- For Display name, enter
Repeat the steps above for another app permission named BlazorAuthDemo.ReadWrite.All
- Still on the same app registration, select the Token configuration blade to the left.
- Select Add optional claim:
-
Select optional claim type, then choose Access.
-
Select the optional claim idtyp. Indicates token type. This claim is the most accurate way for an API to determine if a token is an app token or an app+user token. This is not issued in tokens issued to users.
-
Select Add to save your changes.
-
- Select Owners
- Click Add Owners
- Select your account This step is required for the Api to show up in the API Permissions step of client.
- Store the App Registration values in the appsettings.development.json and for security reasons make sure to not check in this file:
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"ClientId": "Client ID from API App Registration",
"TenantId": "Tenant ID from APO App Registration"
},
- When deployed to Azure, the API should read these settings from Environment Variables or ideally, from KeyVault
-
Navigate to the Azure portal
-
Select the App Registrations, then select New registration.
-
In the Register an application page that appears, enter your application's registration information:
- In the Name section, enter a meaningful application name that will be displayed to users of the app:
BlazorAuthDemo.Client
- Under Supported account types, select Accounts in this organizational directory only
- Select Register to create the application.
- In the Name section, enter a meaningful application name that will be displayed to users of the app:
-
In the Overview blade, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
-
In the app's registration screen, select the Authentication blade to the left.
-
If you don't have a platform added, select Add a platform and select the Web option.
- In the Redirect URI section enter the following redirect URI:
https://localhost:44321/signin-oidc
- In the Front-channel logout URL section, set it to
https://localhost:44321/signout-oidc.
- Check ID tokens (used for implicit and hybrid flows)
- Click Configure to save your changes.
- In the Redirect URI section enter the following redirect URI:
-
In the app's registration screen, select the Certificates & secrets blade in the left to open the page where you can generate secrets and upload certificates.
-
In the Client secrets section, select New client secret:
- Type a key description (for instance
BlazorClientDemoSecret
). - Select one of the available key durations (6 months, 12 months or Custom) as per your security posture.
- The generated key value will be displayed when you select the Add button. Copy and save the generated value for use in later steps.
- You'll need this key later in your code's configuration files. This key value will not be displayed again, and is not retrievable by any other means, so make sure to note it from the Azure portal before navigating to any other screen or blade.
- Type a key description (for instance
- Since this app signs-in users, we will now proceed to select delegated permissions, which is is required by apps signing-in users.
- In the app's registration screen, select the API permissions blade in the left to open the page where we add access to the APIs that your application needs:
- Select the Add a permission button and then:
- Ensure that the My APIs tab is selected.
- In the list of APIs, select the API
BlazorAuthDemo.Api
.- Since this app signs-in users, we will now proceed to select delegated permissions, which is requested by apps that signs-in users.
- In the Delegated permissions section, select BlazorAuthDemo.Read, BlazorAuthDemo.ReadWrite in the list. Use the search box if necessary.
- Select the Add permissions button at the bottom.
- Still on the same app registration, select the Token configuration blade to the left.
- Select Add optional claim:
- Select optional claim type, then choose ID.
- Select the optional claim acct.
Provides user's account status in tenant. If the user is a member of the tenant, the value is 0. If they're a guest, the value is 1.
- Select Add to save your changes.
- Store the App Registration values in the appsettings.development.json and for security reasons make sure to not check in this file:
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "https://portal.azure.com/",
"ClientId": "Client ID from API App Registration",
"TenantId": "Tenant ID from API App Registration"
"ClientSecret": "Client Secret from App Registrations",
"CallbackPath": "/signin-oidc",
"SignedOutCallbackPath": "/signout-oidc"
},
"BlazorAuthDemo.Api": {
"Scopes": [ "api://{id}/BlazorAuthDemo.ReadWrite", "api://{id}/BlazorAuthDemo.Read" ],
"BaseUrl": "https://localhost:7251/",
"RelativePath": "api"
},
- When deployed to Azure, the API should read these settings from Environment Variables or ideally, from KeyVault
NOTE: the BaseUrl should be the same as the url your api is running on.