You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/security/blazor/webassembly/hosted-with-azure-active-directory-b2c.md
+37-23Lines changed: 37 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,22 +23,30 @@ This article describes how to create a Blazor WebAssembly standalone app that us
23
23
24
24
### Create a tenant
25
25
26
-
Follow the guidance in [Tutorial: Create an Azure Active Directory B2C tenant](/azure/active-directory-b2c/tutorial-create-tenant) to create an AAD B2C tenant and record the following information:
26
+
Follow the guidance in [Tutorial: Create an Azure Active Directory B2C tenant](/azure/active-directory-b2c/tutorial-create-tenant) to create an AAD B2C tenant.
27
+
28
+
Record the following information:
27
29
28
30
* AAD B2C instance (for example, `https://contoso.b2clogin.com/`, which includes the trailing slash)
29
31
* AAD B2C Tenant domain (for example, `contoso.onmicrosoft.com`)
30
32
31
33
### Register a server API app
32
34
33
-
Follow the guidance in [Tutorial: Register an application in Azure Active Directory B2C](/azure/active-directory-b2c/tutorial-register-applications) to register an AAD app for the *Server API app* in the **Azure Active Directory** > **App registrations** area of the Azure portal:
35
+
Follow the guidance in [Tutorial: Register an application in Azure Active Directory B2C](/azure/active-directory-b2c/tutorial-register-applications) to register an AAD app for the *Server API app*:
34
36
35
-
1.Select**New registration**.
37
+
1.In **Azure Active Directory** > **App registrations**, select**New registration**.
36
38
1. Provide a **Name** for the app (for example, **Blazor Server AAD B2C**).
37
-
1. For **Supported account types**, select **Accounts in any organizational directory or any identity provider. For authenticating users with Azure AD B2C.** (multi-tenant) for this experience.
39
+
1. For **Supported account types**, select the multi-tenant option: **Accounts in any organizational directory or any identity provider. For authenticating users with Azure AD B2C.**
38
40
1. The *Server API app* doesn't require a **Redirect URI** in this scenario, so leave the drop down set to **Web** and don't enter a redirect URI.
39
41
1. Confirm that **Permissions** > **Grant admin concent to openid and offline_access permissions** is enabled.
40
42
1. Select **Register**.
41
43
44
+
Record the following information:
45
+
46
+
**Server API app* Application ID (Client ID) (for example, `11111111-1111-1111-1111-111111111111`)
47
+
* Directory ID (Tenant ID) (for example, `222222222-2222-2222-2222-222222222222`)
48
+
* AAD Tenant domain (for example, `contoso.onmicrosoft.com`) – The domain is available as the **Publisher domain** in the **Branding** blade of the Azure portal for the registered app.
49
+
42
50
In **Expose an API**:
43
51
44
52
1. Select **Add a scope**.
@@ -51,23 +59,22 @@ In **Expose an API**:
51
59
52
60
Record the following information:
53
61
54
-
**Server API app* Application ID (Client ID) (for example, `11111111-1111-1111-1111-111111111111`)
55
62
* App ID URI (for example, `https://contoso.onmicrosoft.com/11111111-1111-1111-1111-111111111111`, `api://11111111-1111-1111-1111-111111111111`, or the custom value that you provided)
56
-
* Directory ID (Tenant ID) (for example, `222222222-2222-2222-2222-222222222222`)
57
-
**Server API app* App ID URI (for example, `https://contoso.onmicrosoft.com/11111111-1111-1111-1111-111111111111`, the Azure portal might default the value to the Client ID)
58
63
* Default scope (for example, `API.Access`)
59
64
60
65
### Register a client app
61
66
62
-
Follow the guidance in [Tutorial: Register an application in Azure Active Directory B2C](/azure/active-directory-b2c/tutorial-register-applications) again to register an AAD app for the *Client app* in the **Azure Active Directory** > **App registrations** area of the Azure portal:
67
+
Follow the guidance in [Tutorial: Register an application in Azure Active Directory B2C](/azure/active-directory-b2c/tutorial-register-applications) again to register an AAD app for the *Client app*:
63
68
64
-
1.Select**New registration**.
69
+
1.In **Azure Active Directory** > **App registrations**, select**New registration**.
65
70
1. Provide a **Name** for the app (for example, **Blazor Client AAD B2C**).
66
-
1. For **Supported account types**, select **Accounts in any organizational directory or any identity provider. For authenticating users with Azure AD B2C.** (multi-tenant) for this experience.
67
-
1. Leave the **Redirect URI** drop down set to **Web**, and provide a redirect URI of `https://localhost:5001/authentication/login-callback`.
71
+
1. For **Supported account types**, select the multi-tenant option: **Accounts in any organizational directory or any identity provider. For authenticating users with Azure AD B2C.**
72
+
1. Leave the **Redirect URI** drop down set to **Web**, and provide the following redirect URI: `https://localhost:5001/authentication/login-callback`
68
73
1. Confirm that **Permissions** > **Grant admin concent to openid and offline_access permissions** is enabled.
69
74
1. Select **Register**.
70
75
76
+
Record the Application ID (Client ID) (for example, `11111111-1111-1111-1111-111111111111`).
77
+
71
78
In **Authentication** > **Platform configurations** > **Web**:
72
79
73
80
1. Confirm the **Redirect URI** of `https://localhost:5001/authentication/login-callback` is present.
1. Confirm that the app has **Microsoft Graph** > **User.Read** permission.
81
87
1. Select **Add a permission** followed by **My APIs**.
82
88
1. Select the *Server API app* from the **Name** column (for example, **Blazor Server AAD B2C**).
83
89
1. Open the **API** list.
84
90
1. Enable access to the API (for example, `API.Access`).
85
91
1. Select **Add permissions**.
86
92
1. Select the **Grant admin content for {TENANT NAME}** button. Select **Yes** to confirm.
87
93
94
+
Record the App ID URI (for example, `https://{ORGANIZATION}.onmicrosoft.com/{SERVER API APP CLIENT ID OR CUSTOM VALUE}`).
95
+
88
96
In **Home** > **Azure AD B2C** > **User flows**:
89
97
90
98
[Create a sign-up and sign-in user flow](/azure/active-directory-b2c/tutorial-create-user-flows)
91
99
92
100
At a minimum, select the **Application claims** > **Display Name** user attribute to populate the `context.User.Identity.Name` in the `LoginDisplay` component (*Shared/LoginDisplay.razor*).
93
101
94
-
Record the following information:
95
-
96
-
* Record the *Client app* Application ID (Client ID) (for example, `33333333-3333-3333-3333-333333333333`).
97
-
* Record the sign-up and sign-in user flow name created for the app (for example, `B2C_1_signupsignin`).
102
+
Record the sign-up and sign-in user flow name created for the app (for example, `B2C_1_signupsignin`).
98
103
99
104
### Create the app
100
105
101
106
Replace the placeholders in the following command with the information recorded earlier and execute the command in a command shell:
102
107
103
108
```dotnetcli
104
-
dotnet new blazorwasm -au IndividualB2C --aad-b2c-instance "{AAD B2C INSTANCE}" --api-client-id "{SERVER API APP CLIENT ID}" --app-id-uri "{SERVER API APP ID URI}" --client-id "{CLIENT APP CLIENT ID}" --default-scope "{DEFAULT SCOPE}" --domain "{DOMAIN}" -ho -ssp "{SIGN UP OR SIGN IN POLICY}" --tenant-id "{TENANT ID}"
109
+
dotnet new blazorwasm -au IndividualB2C --aad-b2c-instance "{AAD B2C INSTANCE}" --api-client-id "{SERVER API APP CLIENT ID}" --app-id-uri "{SERVER API APP ID URI}" --client-id "{CLIENT APP CLIENT ID}" --default-scope "{DEFAULT SCOPE}" --domain "{TENANT DOMAIN}" -ho -ssp "{SIGN UP OR SIGN IN POLICY}" --tenant-id "{TENANT ID}"
105
110
```
106
111
107
112
To specify the output location, which creates a project folder if it doesn't exist, include the output option in the command with a path (for example, `-o BlazorSample`). The folder name also becomes part of the project's name.
108
113
109
114
> [!NOTE]
110
115
> Pass the App ID URI to the `app-id-uri` option, but note a configuration change might be required in the client app, which is described in the [Access token scopes](#access-token-scopes) section.
116
+
>
117
+
> Additionally, the scope set up by the Hosted Blazor template might have the App ID URI host repeated. Confirm that the scope configured for the `DefaultAccessTokenScopes` collection is correct in `Program.Main` (*Program.cs*) of the *Client app*.
111
118
112
119
## Server app configuration
113
120
@@ -148,6 +155,10 @@ By default, the `User.Identity.Name` isn't populated.
148
155
To configure the app to receive the value from the `name` claim type, configure the [TokenValidationParameters.NameClaimType](xref:Microsoft.IdentityModel.Tokens.TokenValidationParameters.NameClaimType) of the <xref:Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerOptions> in `Startup.ConfigureServices`:
Support for authenticating users is registered in the service container with the `AddMsalAuthentication` extension method provided by the `Microsoft.Authentication.WebAssembly.Msal` package. This method sets up all of the services required for the app to interact with the Identity Provider (IP).
252
+
Support for authenticating users is registered in the service container with the `AddMsalAuthentication` extension method provided by the `Microsoft.Authentication.WebAssembly.Msal` package. This method sets up the services required for the app to interact with the Identity Provider (IP).
242
253
243
254
*Program.cs*:
244
255
@@ -257,7 +268,7 @@ Configuration is supplied by the *wwwroot/appsettings.json* file:
257
268
```json
258
269
{
259
270
"AzureAdB2C": {
260
-
"Authority": "{AAD B2C INSTANCE}{DOMAIN}/{SIGN UP OR SIGN IN POLICY}",
271
+
"Authority": "{AAD B2C INSTANCE}{TENANT DOMAIN}/{SIGN UP OR SIGN IN POLICY}",
261
272
"ClientId": "{CLIENT APP CLIENT ID}",
262
273
"ValidateAuthority": false
263
274
}
@@ -342,7 +353,10 @@ For more information, see the following sections of the *Additional scenarios* a
0 commit comments