Skip to content

Commit e36e684

Browse files
authored
Topic update (#18158)
1 parent 5e97ec4 commit e36e684

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

aspnetcore/fundamentals/http-context.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to access HttpContext in ASP.NET Core.
55
monikerRange: '>= aspnetcore-2.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 12/03/2019
8+
ms.date: 5/5/2020
99
no-loc: [Blazor, "Identity", "Let's Encrypt", Razor, SignalR]
1010
uid: fundamentals/httpcontext
1111
---
@@ -158,3 +158,8 @@ public class EmailController : Controller
158158
...
159159
}
160160
}
161+
```
162+
163+
## Blazor and shared state
164+
165+
[!INCLUDE[](~/includes/blazor-security/blazor-shared-state.md)]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Blazor server apps live in server memory. That means that there are multiple apps hosted within the same process. For each app session, Blazor starts a circuit with its own DI container scope. That means that scoped services are unique per Blazor session.
2+
3+
> [!WARNING]
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.
5+
6+
You can use stateful singleton services in Blazor apps if they are specifically designed for it. For example, it's ok to use a memory cache as a singleton because it requires a key to access a given entry, assuming users don't have control of what cache keys are used.
7+
8+
**Additionally, again for security reasons, you must not use <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor> within Blazor apps.** Blazor apps run outside of the context of the ASP.NET Core pipeline and the <xref:Microsoft.AspNetCore.Http.HttpContext> isn't guaranteed to be available within the <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor>, nor it is guaranteed to be holding the context that started the Blazor app.
9+
10+
The recommended way to pass request state to the Blazor app is through parameters to the root component in the initial rendering of the app:
11+
12+
* Define a class with all the data you want to pass to the Blazor app.
13+
* Populate that data from the Razor page using the <xref:Microsoft.AspNetCore.Http.HttpContext> available at that time.
14+
* Pass the data to the Blazor app as a parameter to the root component (App).
15+
* Define a parameter in the root component to hold the data being passed to the app.
16+
* Use the user-specific data within the app; or alternatively, copy that data into a scoped service within <xref:Microsoft.AspNetCore.Components.ComponentBase.OnInitializedAsync%2A> so that it can be used across the app.
17+
18+
For more information and example code, see <xref:security/blazor/server/additional-scenarios#pass-tokens-to-a-blazor-server-app>.

aspnetcore/security/blazor/server/threat-mitigation.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ In constrained environments, such as inside corporate networks or intranets, som
2424
* Doesn't apply in the constrained environment.
2525
* Isn't worth the cost to implement because the security risk is low in a constrained environment.
2626

27+
## Blazor and shared state
28+
29+
[!INCLUDE[](~/includes/blazor-security/blazor-shared-state.md)]
30+
2731
## Resource exhaustion
2832

2933
Resource exhaustion can occur when a client interacts with the server and causes the server to consume excessive resources. Excessive resource consumption primarily affects:

0 commit comments

Comments
 (0)