Skip to content

Commit 6d22c8d

Browse files
authored
Blazor WASM security updates for 5.0 (#20138)
1 parent 95e9973 commit 6d22c8d

18 files changed

Lines changed: 299 additions & 57 deletions

aspnetcore/blazor/debug.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ To debug a Blazor WebAssembly app in Visual Studio:
7777
> [!NOTE]
7878
> **Start Without Debugging** (<kbd>Ctrl</kbd>+<kbd>F5</kbd>) isn't supported. When the app is run in Debug configuration, debugging overhead always results in a small performance reduction.
7979
80-
1. In the *Client* app, set a breakpoint on the `currentCount++;` line in `Pages/Counter.razor`.
80+
1. In the `*Client*` app, set a breakpoint on the `currentCount++;` line in `Pages/Counter.razor`.
8181
1. In the browser, navigate to `Counter` page and select the **Click me** button to hit the breakpoint.
8282
1. In Visual Studio, inspect the value of the `currentCount` field in the **Locals** window.
8383
1. Press <kbd>F5</kbd> to continue execution.
@@ -164,7 +164,7 @@ For information on using a custom app base path for Blazor WebAssembly apps, see
164164

165165
1. The standalone app is launched, and a debugging browser is opened.
166166

167-
1. In the *Client* app, set a breakpoint on the `currentCount++;` line in `Pages/Counter.razor`.
167+
1. In the `*Client*` app, set a breakpoint on the `currentCount++;` line in `Pages/Counter.razor`.
168168

169169
1. In the browser, navigate to `Counter` page and select the **Click me** button to hit the breakpoint.
170170

@@ -269,7 +269,7 @@ To debug a Blazor WebAssembly app in Visual Studio for Mac:
269269
> [!IMPORTANT]
270270
> Google Chrome or Microsoft Edge must be the selected browser for the debugging session.
271271
272-
1. In the *Client* app, set a breakpoint on the `currentCount++;` line in `Pages/Counter.razor`.
272+
1. In the `*Client*` app, set a breakpoint on the `currentCount++;` line in `Pages/Counter.razor`.
273273
1. In the browser, navigate to `Counter` page and select the **Click me** button to hit the breakpoint:
274274
1. In Visual Studio, inspect the value of the `currentCount` field in the **Locals** window.
275275
1. Press <kbd>&#8984;</kbd>+<kbd>&#8617;</kbd> to continue execution.

aspnetcore/blazor/security/webassembly/additional-scenarios.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ For a Blazor app based on the Blazor WebAssembly Hosted project template, <xref:
161161

162162
### Graph API example
163163

164-
In the following example, a named <xref:System.Net.Http.HttpClient> for Graph API is used to obtain a user's mobile phone number to process a call. After adding the Microsoft Graph API `User.Read` permission in the AAD area of the Azure portal, the scope is configured for the named client in the standalone app or Client app of a hosted Blazor solution.
164+
In the following example, a named <xref:System.Net.Http.HttpClient> for Graph API is used to obtain a user's mobile phone number to process a call. After adding the Microsoft Graph API `User.Read` permission in the AAD area of the Azure portal, the scope is configured for the named client in the standalone app or *`Client`* app of a hosted Blazor solution.
165165

166166
> [!NOTE]
167167
> The example in this section obtains Graph API data for the user in *component code*. To create user claims from Graph API, see the following resources:
@@ -1054,7 +1054,7 @@ After following the guidance in one of the hosted Blazor WebAssembly app topics,
10541054
* Prerenders paths for which authorization isn't required.
10551055
* Doesn't prerender paths for which authorization is required.
10561056

1057-
In the Client app's `Program` class (`Program.cs`), factor common service registrations into a separate method (for example, `ConfigureCommonServices`):
1057+
In the *`Client`* app's `Program` class (`Program.cs`), factor common service registrations into a separate method (for example, `ConfigureCommonServices`):
10581058

10591059
```csharp
10601060
public class Program
@@ -1114,7 +1114,7 @@ app.UseEndpoints(endpoints =>
11141114
});
11151115
```
11161116

1117-
In the server app, create a `Pages` folder if it doesn't exist. Create a `_Host.cshtml` page inside the server app's `Pages` folder. Paste the contents from the Client app's `wwwroot/index.html` file into the `Pages/_Host.cshtml` file. Update the file's contents:
1117+
In the server app, create a `Pages` folder if it doesn't exist. Create a `_Host.cshtml` page inside the server app's `Pages` folder. Paste the contents from the *`Client`* app's `wwwroot/index.html` file into the `Pages/_Host.cshtml` file. Update the file's contents:
11181118

11191119
* Add `@page "_Host"` to the top of the file.
11201120
* Replace the `<app>Loading...</app>` tag with the following:

aspnetcore/blazor/security/webassembly/azure-active-directory-groups-and-roles.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The guidance in this article applies to the Blazor WebAssembly AAD deployment sc
3333

3434
A [Microsoft Graph API](/graph/use-the-api) call is required for any app user with more than five AAD Administrator role and security group memberships.
3535

36-
To permit Graph API calls, give the standalone or client app of a hosted Blazor solution any of the following [Graph API permissions](/graph/permissions-reference) in the Azure portal:
36+
To permit Graph API calls, give the standalone or *`Client`* app of a hosted Blazor solution any of the following [Graph API permissions](/graph/permissions-reference) in the Azure portal:
3737

3838
* `Directory.Read.All`
3939
* `Directory.ReadWrite.All`
@@ -72,7 +72,7 @@ public class CustomUserAccount : RemoteUserAccount
7272
}
7373
```
7474

75-
In the standalone app or the client app of a hosted Blazor solution, create a custom <xref:Microsoft.AspNetCore.Components.WebAssembly.Authentication.AuthorizationMessageHandler> class. Use the correct scope (permission) for Graph API calls that obtain role and group information.
75+
In the standalone app or the *`Client`* app of a hosted Blazor solution, create a custom <xref:Microsoft.AspNetCore.Components.WebAssembly.Authentication.AuthorizationMessageHandler> class. Use the correct scope (permission) for Graph API calls that obtain role and group information.
7676

7777
`GraphAPIAuthorizationMessageHandler.cs`:
7878

@@ -234,7 +234,7 @@ There's no need to provide code to remove the original `groups` claim, if presen
234234
>
235235
> General coverage for this approach is found in the <xref:blazor/security/webassembly/additional-scenarios#custom-authorizationmessagehandler-class> article.
236236
237-
Register the factory in `Program.Main` (`Program.cs`) of the standalone app or client app of a hosted Blazor solution. Consent to the `Directory.Read.All` permission scope as an additional scope for the app:
237+
Register the factory in `Program.Main` (`Program.cs`) of the standalone app or *`Client`* app of a hosted Blazor solution. Consent to the `Directory.Read.All` permission scope as an additional scope for the app:
238238

239239
```csharp
240240
builder.Services.AddMsalAuthentication<RemoteAuthenticationState,
@@ -635,9 +635,9 @@ The following example assumes that an app is configured with two roles:
635635
636636
The single `roles` claim sent by AAD presents the user-defined roles as the `appRoles`'s `value`s in a JSON array. The app must convert the JSON array of roles into individual `role` claims.
637637

638-
The `CustomUserFactory` shown in the [User-defined groups and AAD Administrator Roles](#user-defined-groups-and-administrator-roles) section is set up to act on a `roles` claim with a JSON array value. Add and register the `CustomUserFactory` in the standalone app or client app of a hosted Blazor solution as shown in the [User-defined groups and AAD Administrator Roles](#user-defined-groups-and-administrator-roles) section. There's no need to provide code to remove the original `roles` claim because it's automatically removed by the framework.
638+
The `CustomUserFactory` shown in the [User-defined groups and AAD Administrator Roles](#user-defined-groups-and-administrator-roles) section is set up to act on a `roles` claim with a JSON array value. Add and register the `CustomUserFactory` in the standalone app or *`Client`* app of a hosted Blazor solution as shown in the [User-defined groups and AAD Administrator Roles](#user-defined-groups-and-administrator-roles) section. There's no need to provide code to remove the original `roles` claim because it's automatically removed by the framework.
639639

640-
In `Program.Main` of the standalone app or client app of a hosted Blazor solution, specify the claim named "`role`" as the role claim:
640+
In `Program.Main` of the standalone app or *`Client`* app of a hosted Blazor solution, specify the claim named "`role`" as the role claim:
641641

642642
```csharp
643643
builder.Services.AddMsalAuthentication(options =>

aspnetcore/blazor/security/webassembly/hosted-with-azure-active-directory-b2c.md

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Follow the guidance in [Tutorial: Register an application in Azure Active Direct
3131
1. Provide a **Name** for the app (for example, **Blazor Server AAD B2C**).
3232
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.**
3333
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.
34-
1. Confirm that **Permissions** > **Grant admin consent to openid and offline_access permissions** is enabled.
34+
1. Confirm that **Permissions** > **Grant admin consent to openid and offline_access permissions** is selected.
3535
1. Select **Register**.
3636

3737
Record the following information:
@@ -58,13 +58,35 @@ The App ID URI might require a special configuration in the client app, which is
5858

5959
### Register a client app
6060

61-
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* and then do the following:
61+
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 and then do the following:
62+
63+
::: moniker range=">= aspnetcore-5.0"
6264

6365
1. In **Azure Active Directory** > **App registrations**, select **New registration**.
6466
1. Provide a **Name** for the app (for example, **Blazor Client AAD B2C**).
6567
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.**
66-
1. Leave the **Redirect URI** drop down set to **Web** and provide the following redirect URI: `https://localhost:{PORT}/authentication/login-callback`. The default port for an app running on Kestrel is 5001. If the app is run on a different Kestrel port, use the app's port. For IIS Express, the randomly generated port for the app can be found in the Server app's properties in the **Debug** panel. Since the app doesn't exist at this point and the IIS Express port isn't known, return to this step after the app is created and update the redirect URI. A remark appears in the [Create the app](#create-the-app) section to remind IIS Express users to update the redirect URI.
67-
1. Confirm that **Permissions** > **Grant admin consent to openid and offline_access permissions** is enabled.
68+
1. Set the **Redirect URI** drop down to **Single-page application (SPA)** and provide the following redirect URI: `https://localhost:{PORT}/authentication/login-callback`. The default port for an app running on Kestrel is 5001. If the app is run on a different Kestrel port, use the app's port. For IIS Express, the randomly generated port for the app can be found in the *`Server`* app's properties in the **Debug** panel. Since the app doesn't exist at this point and the IIS Express port isn't known, return to this step after the app is created and update the redirect URI. A remark appears in the [Create the app](#create-the-app) section to remind IIS Express users to update the redirect URI.
69+
1. Confirm that **Permissions** > **Grant admin consent to openid and offline_access permissions** is selected.
70+
1. Select **Register**.
71+
72+
1. Record the Application (client) ID (for example, `4369008b-21fa-427c-abaa-9b53bf58e538`).
73+
74+
In **Authentication** > **Platform configurations** > **Single-page application (SPA)**:
75+
76+
1. Confirm the **Redirect URI** of `https://localhost:{PORT}/authentication/login-callback` is present.
77+
1. For **Implicit grant**, ensure that the check boxes for **Access tokens** and **ID tokens** are **not** selected.
78+
1. The remaining defaults for the app are acceptable for this experience.
79+
1. Select the **Save** button.
80+
81+
::: moniker-end
82+
83+
::: moniker range="< aspnetcore-5.0"
84+
85+
1. In **Azure Active Directory** > **App registrations**, select **New registration**.
86+
1. Provide a **Name** for the app (for example, **Blazor Client AAD B2C**).
87+
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.**
88+
1. Leave the **Redirect URI** drop down set to **Web** and provide the following redirect URI: `https://localhost:{PORT}/authentication/login-callback`. The default port for an app running on Kestrel is 5001. If the app is run on a different Kestrel port, use the app's port. For IIS Express, the randomly generated port for the app can be found in the *`Server`* app's properties in the **Debug** panel. Since the app doesn't exist at this point and the IIS Express port isn't known, return to this step after the app is created and update the redirect URI. A remark appears in the [Create the app](#create-the-app) section to remind IIS Express users to update the redirect URI.
89+
1. Confirm that **Permissions** > **Grant admin consent to openid and offline_access permissions** is selected.
6890
1. Select **Register**.
6991

7092
Record the Application (client) ID (for example, `4369008b-21fa-427c-abaa-9b53bf58e538`).
@@ -76,6 +98,8 @@ In **Authentication** > **Platform configurations** > **Web**:
7698
1. The remaining defaults for the app are acceptable for this experience.
7799
1. Select the **Save** button.
78100

101+
::: moniker-end
102+
79103
In **API permissions**:
80104

81105
1. Select **Add a permission** followed by **My APIs**.
@@ -105,7 +129,7 @@ dotnet new blazorwasm -au IndividualB2C --aad-b2c-instance "{AAD B2C INSTANCE}"
105129
| ----------------------------- | ----------------------------------------------------- | -------------------------------------- |
106130
| `{AAD B2C INSTANCE}` | Instance | `https://contoso.b2clogin.com/` |
107131
| `{APP NAME}` | &mdash; | `BlazorSample` |
108-
| `{CLIENT APP CLIENT ID}` | Application (client) ID for the *Client app* | `4369008b-21fa-427c-abaa-9b53bf58e538` |
132+
| `{CLIENT APP CLIENT ID}` | Application (client) ID for the *`Client`* app | `4369008b-21fa-427c-abaa-9b53bf58e538` |
109133
| `{DEFAULT SCOPE}` | Scope name | `API.Access` |
110134
| `{SERVER API APP CLIENT ID}` | Application (client) ID for the *Server API app* | `41451fa7-82d9-4673-8fa5-69eff5a761fd` |
111135
| `{SERVER API APP ID URI}` | Application ID URI ([see note](#access-token-scopes)) | `41451fa7-82d9-4673-8fa5-69eff5a761fd` |
@@ -117,16 +141,22 @@ The output location specified with the `-o|--output` option creates a project fo
117141
> [!NOTE]
118142
> 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.
119143
>
120-
> 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*.
144+
> 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.
145+
146+
::: moniker range=">= aspnetcore-5.0"
147+
148+
[!INCLUDE[](~/includes/blazor-security/additional-scopes-hosted-nonAAD.md)]
149+
150+
::: moniker-end
121151

122152
> [!NOTE]
123-
> In the Azure portal, the *Client app's* **Authentication** > **Platform configurations** > **Web** > **Redirect URI** is configured for port 5001 for apps that run on the Kestrel server with default settings.
153+
> In the Azure portal, the *`Client`* app's platform configuration **Redirect URI** is configured for port 5001 for apps that run on the Kestrel server with default settings.
124154
>
125-
> If the *Client app* is run on a random IIS Express port, the port for the app can be found in the *Server API app's* properties in the **Debug** panel.
155+
> If the *`Client`* app is run on a random IIS Express port, the port for the app can be found in the *Server API app's* properties in the **Debug** panel.
126156
>
127-
> If the port wasn't configured earlier with the *Client app's* known port, return to the *Client app's* registration in the Azure portal and update the redirect URI with the correct port.
157+
> If the port wasn't configured earlier with the *`Client`* app's known port, return to the *`Client`* app's registration in the Azure portal and update the redirect URI with the correct port.
128158
129-
## Server app configuration
159+
## *`Server`* app configuration
130160

131161
*This section pertains to the solution's **`Server`** app.*
132162

@@ -227,7 +257,7 @@ public class WeatherForecastController : ControllerBase
227257
}
228258
```
229259

230-
## Client app configuration
260+
## *`Client`* app configuration
231261

232262
*This section pertains to the solution's **`Client`** app.*
233263

@@ -318,6 +348,12 @@ builder.Services.AddMsalAuthentication(options =>
318348
});
319349
```
320350

351+
Specify additional scopes with `AdditionalScopesToConsent`:
352+
353+
```csharp
354+
options.ProviderOptions.AdditionalScopesToConsent.Add("{ADDITIONAL SCOPE URI}");
355+
```
356+
321357
[!INCLUDE[](~/includes/blazor-security/azure-scope.md)]
322358

323359
For more information, see the following sections of the *Additional scenarios* article:

0 commit comments

Comments
 (0)