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/blazor/globalization-localization.md
+2
Original file line number
Diff line number
Diff line change
@@ -496,6 +496,8 @@ Use the `CultureExample1` component shown in the [Demonstration component](#demo
496
496
497
497
## Dynamically set the server-side culture by user preference
498
498
499
+
<!-- UPDATE 8.0 Requires a new approach for BWA apps -->
500
+
499
501
:::moniker range=">= aspnetcore-8.0"
500
502
501
503
Examples of locations where an app might store a user's preference include in [browser local storage](https://developer.mozilla.org/docs/Web/API/Window/localStorage) (common for client-side rendering), in a localization cookie or database (common for server-side rendering), or in an external service attached to an external database and accessed by a [web API](xref:blazor/call-web-api).
This article explains how to use a [Content Security Policy (CSP)](https://developer.mozilla.org/docs/Web/HTTP/CSP) with ASP.NET Core Blazor apps to help protect against [Cross-Site Scripting (XSS)](xref:security/cross-site-scripting) attacks.
[Cross-Site Scripting (XSS)](xref:security/cross-site-scripting) is a security vulnerability where an attacker places one or more malicious client-side scripts into an app's rendered content. A CSP helps protect against XSS attacks by informing the browser of valid:
18
20
19
21
* Sources for loaded content, including scripts, stylesheets, images, and plugins.
@@ -27,7 +29,7 @@ CSP is supported in most modern desktop and mobile browsers, including Chrome, E
27
29
28
30
## Policy directives
29
31
30
-
Minimally, specify the following directives and sources for Blazor apps. Add additional directives and sources as needed. The following directives are used in the *Apply the policy* section of this article, where example security policies for Blazor WebAssembly and Blazor Server are provided:
32
+
Minimally, specify the following directives and sources for Blazor apps. Add additional directives and sources as needed. The following directives are used in the *Apply the policy* section of this article, where example security policies for Blazor apps are provided:
31
33
32
34
:::moniker range=">= aspnetcore-8.0"
33
35
@@ -39,10 +41,10 @@ Minimally, specify the following directives and sources for Blazor apps. Add add
39
41
*[object-src](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/object-src): Indicates valid sources for the `<object>`, `<embed>`, and `<applet>` tags. Specify `none` to prevent all URL sources.
40
42
*[script-src](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/script-src): Indicates valid sources for scripts.
41
43
* Specify `self` to indicate that the app's origin, including the scheme and port number, is a valid source.
42
-
* In a Blazor WebAssembly app:
43
-
* Specify [`wasm-unsafe-eval`](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_webassembly_execution) to permit the Blazor WebAssembly Mono runtime to function.
44
+
* In a client-side Blazor app:
45
+
* Specify [`wasm-unsafe-eval`](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_webassembly_execution) to permit the client-side Blazor Mono runtime to function.
44
46
* Specify any additional hashes to permit your required *non-framework scripts* to load.
45
-
* In a Blazor Server app, specify hashes to permit required scripts to load.
47
+
* In a server-side Blazor app, specify hashes to permit required scripts to load.
46
48
*[style-src](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/style-src): Indicates valid sources for stylesheets.
47
49
* Specify `self` to indicate that the app's origin, including the scheme and port number, is a valid source.
48
50
* If the app uses inline styles, specify `unsafe-inline` to allow the use of your inline styles.
@@ -60,10 +62,10 @@ Minimally, specify the following directives and sources for Blazor apps. Add add
60
62
*[object-src](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/object-src): Indicates valid sources for the `<object>`, `<embed>`, and `<applet>` tags. Specify `none` to prevent all URL sources.
61
63
*[script-src](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/script-src): Indicates valid sources for scripts.
62
64
* Specify `self` to indicate that the app's origin, including the scheme and port number, is a valid source.
63
-
* In a Blazor WebAssembly app:
64
-
* Specify `unsafe-eval` to permit the Blazor WebAssembly Mono runtime to function.
65
+
* In a client-side Blazor app:
66
+
* Specify `unsafe-eval` to permit the client-side Blazor Mono runtime to function.
65
67
* Specify any additional hashes to permit your required *non-framework scripts* to load.
66
-
* In a Blazor Server app, specify hashes to permit required scripts to load.
68
+
* In a server-side Blazor app, specify hashes to permit required scripts to load.
67
69
*[style-src](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/style-src): Indicates valid sources for stylesheets.
68
70
* Specify `self` to indicate that the app's origin, including the scheme and port number, is a valid source.
69
71
* If the app uses inline styles, specify `unsafe-inline` to allow the use of your inline styles.
@@ -82,10 +84,10 @@ Minimally, specify the following directives and sources for Blazor apps. Add add
82
84
*[script-src](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/script-src): Indicates valid sources for scripts.
83
85
* Specify the `https://stackpath.bootstrapcdn.com/` host source for Bootstrap scripts.
84
86
* Specify `self` to indicate that the app's origin, including the scheme and port number, is a valid source.
85
-
* In a Blazor WebAssembly app:
86
-
* Specify `unsafe-eval` to permit the Blazor WebAssembly Mono runtime to function.
87
+
* In a client-side Blazor app:
88
+
* Specify `unsafe-eval` to permit the client-side Blazor Mono runtime to function.
87
89
* Specify any additional hashes to permit your required *non-framework scripts* to load.
88
-
* In a Blazor Server app, specify hashes to permit required scripts to load.
90
+
* In a server-side Blazor app, specify hashes to permit required scripts to load.
89
91
*[style-src](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/style-src): Indicates valid sources for stylesheets.
90
92
* Specify the `https://stackpath.bootstrapcdn.com/` host source for Bootstrap stylesheets.
91
93
* Specify `self` to indicate that the app's origin, including the scheme and port number, is a valid source.
@@ -105,14 +107,14 @@ Minimally, specify the following directives and sources for Blazor apps. Add add
105
107
*[script-src](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/script-src): Indicates valid sources for scripts.
106
108
* Specify the `https://stackpath.bootstrapcdn.com/` host source for Bootstrap scripts.
107
109
* Specify `self` to indicate that the app's origin, including the scheme and port number, is a valid source.
108
-
* In a Blazor WebAssembly app:
110
+
* In a client-side Blazor app:
109
111
* Specify hashes to permit required scripts to load.
110
112
* Specify `unsafe-eval` to use `eval()` and methods for creating code from strings.
111
-
* In a Blazor Server app, specify hashes to permit required scripts to load.
113
+
* In a server-side Blazor app, specify hashes to permit required scripts to load.
112
114
*[style-src](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/style-src): Indicates valid sources for stylesheets.
113
115
* Specify the `https://stackpath.bootstrapcdn.com/` host source for Bootstrap stylesheets.
114
116
* Specify `self` to indicate that the app's origin, including the scheme and port number, is a valid source.
115
-
* Specify `unsafe-inline` to allow the use of inline styles. The inline declaration is required for the UI in Blazor Server apps for reconnecting the client and server after the initial request. In a future release, inline styling might be removed so that `unsafe-inline` is no longer required.
117
+
* Specify `unsafe-inline` to allow the use of inline styles. The inline declaration is required for the UI for reconnecting the client and server after the initial request. In a future release, inline styling might be removed so that `unsafe-inline` is no longer required.
116
118
*[upgrade-insecure-requests](https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Security-Policy/upgrade-insecure-requests): Indicates that content URLs from insecure (HTTP) sources should be acquired securely over HTTPS.
117
119
118
120
:::moniker-end
@@ -133,13 +135,13 @@ Use a `<meta>` tag to apply the policy:
133
135
134
136
* Set the value of the `http-equiv` attribute to `Content-Security-Policy`.
135
137
* Place the directives in the `content` attribute value. Separate directives with a semicolon (`;`).
136
-
* Always place the `meta` tag in the `<head>` content.
138
+
* Always place the `meta` tag in the [`<head>` content](xref:blazor/project-structure#location-of-head-and-body-content).
137
139
138
-
The following sections show example policies for Blazor WebAssembly and Blazor Server. These examples are versioned with this article for each release of Blazor. To use a version appropriate for your release, select the document version with the **Version** dropdown selector on this webpage.
140
+
The following sections show example policies. These examples are versioned with this article for each release of Blazor. To use a version appropriate for your release, select the document version with the **Version** dropdown selector on this webpage.
139
141
140
-
### Blazor WebAssembly
142
+
### Client-side Blazor apps
141
143
142
-
In the `<head>` content of the `wwwroot/index.html` host page, apply the directives described in the *Policy directives* section:
144
+
In the [`<head>` content](xref:blazor/project-structure#location-of-head-and-body-content), apply the directives described in the *Policy directives* section:
143
145
144
146
:::moniker range=">= aspnetcore-8.0"
145
147
@@ -189,7 +191,7 @@ In the `<head>` content of the `wwwroot/index.html` host page, apply the directi
189
191
```
190
192
191
193
> [!NOTE]
192
-
> The `sha256-v8v3RKRPmN4odZ1CWM5gw80QKPCCWMcpNeOmimNL2AA=` hash represents the [inline](https://github.com/dotnet/aspnetcore/blob/57501251222b199597b9ac16888f362a69eb13c1/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts#L212) script that's used for Blazor WebAssembly. This may be removed in the future.
194
+
> The `sha256-v8v3RKRPmN4odZ1CWM5gw80QKPCCWMcpNeOmimNL2AA=` hash represents the [inline](https://github.com/dotnet/aspnetcore/blob/57501251222b199597b9ac16888f362a69eb13c1/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts#L212) script that's used for client-side Blazor apps. This may be removed in the future.
193
195
194
196
:::moniker-end
195
197
@@ -241,9 +243,9 @@ Add additional `script-src` and `style-src` hashes as required by the app. Durin
241
243
242
244
The particular script associated with the error is displayed in the console next to the error.
243
245
244
-
### Blazor Server
246
+
### Server-side Blazor apps
245
247
246
-
In the `<head>` markup ([location of `<head>` content](xref:blazor/project-structure#location-of-head-and-body-content)), apply the directives described in the *Policy directives* section:
248
+
In the [`<head>` content](xref:blazor/project-structure#location-of-head-and-body-content), apply the directives described in the *Policy directives* section:
**Don't use <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor>/<xref:Microsoft.AspNetCore.Http.HttpContext> directly or indirectly in the Razor components of Blazor Server apps.** Blazor apps run outside of the ASP.NET Core pipeline context. The <xref:Microsoft.AspNetCore.Http.HttpContext> isn't guaranteed to be available within the <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor>, and <xref:Microsoft.AspNetCore.Http.HttpContext> isn't guaranteed to hold the context that started the Blazor app.
1
+
**Don't use <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor>/<xref:Microsoft.AspNetCore.Http.HttpContext> directly or indirectly in the Razor components of server-side Blazor apps.** Blazor apps run outside of the ASP.NET Core pipeline context. The <xref:Microsoft.AspNetCore.Http.HttpContext> isn't guaranteed to be available within the <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor>, and <xref:Microsoft.AspNetCore.Http.HttpContext> isn't guaranteed to hold the context that started the Blazor app.
2
2
3
-
The recommended approach for passing request state to the Blazor app is through root component parameters during the app's initial rendering. Alternatively, the app can copy the data into a scoped service in the root component's initialization lifecycle event for use across the app. For more information, see <xref:blazor/security/server/additional-scenarios#pass-tokens-to-a-blazor-server-app>.
3
+
The recommended approach for passing request state to the Blazor app is through root component parameters during the app's initial rendering. Alternatively, the app can copy the data into a scoped service in the root component's initialization lifecycle event for use across the app. For more information, see <xref:blazor/security/server/additional-scenarios#pass-tokens-to-a-server-side-blazor-app>.
4
4
5
-
A critical aspect of Blazor Server security is that the user attached to a given circuit might become updated at some point after the Blazor circuit is established but the <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor>***isn't updated***. For more information on addressing this situation with custom services, see <xref:blazor/security/server/additional-scenarios#circuit-handler-to-capture-users-for-custom-services>.
5
+
A critical aspect of server-side Blazor security is that the user attached to a given circuit might become updated at some point after the Blazor circuit is established but the <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor>***isn't updated***. For more information on addressing this situation with custom services, see <xref:blazor/security/server/additional-scenarios#circuit-handler-to-capture-users-for-custom-services>.
Copy file name to clipboardExpand all lines: aspnetcore/blazor/security/includes/shared-state.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
Blazor server apps live in server memory, and multiple app sessions are hosted within the same process. For each app session, Blazor starts a circuit with its own dependency injection container scope, thus scoped services are unique per Blazor session.
1
+
Server-side Blazor apps live in server memory, and multiple app sessions are hosted within the same process. For each app session, Blazor starts a circuit with its own dependency injection container scope, thus scoped services are unique per Blazor session.
2
2
3
3
> [!WARNING]
4
4
> We don't recommend apps on the same server share state using singleton services unless extreme care is taken, as this can introduce security vulnerabilities, such as leaking user state across circuits.
0 commit comments