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
<xref:Microsoft.AspNetCore.Http.IHttpContextAccessor> generally should be avoided with interactive rendering because a valid <xref:Microsoft.AspNetCore.Http.HttpContext> isn't always available.
@@ -11,7 +25,14 @@
11
25
publicHttpContext?HttpContext { get; set; }
12
26
```
13
27
14
-
For scenarios where the <xref:Microsoft.AspNetCore.Http.HttpContext> is required in interactive components, we recommend flowing the data via persistent component state from the server. For more information, see <xref:blazor/security/additional-scenarios#pass-tokens-to-a-server-side-blazor-app>.
28
+
During interactive rendering, an <xref:Microsoft.AspNetCore.Http.HttpContext> instance might not even exist. For scenarios where the <xref:Microsoft.AspNetCore.Http.HttpContext> is required in interactive components, we recommend flowing context data with [persistent component state](xref:blazor/components/prerender#persist-prerendered-state) from the server.
29
+
30
+
For additional context in *advanced* edge cases†, see the discussion in the following articles:
31
+
32
+
*[HttpContext is valid in Interactive Server Rendering Blazor page (`dotnet/AspNetCore.Docs`#34301)](https://github.com/dotnet/AspNetCore.Docs/issues/34301)
33
+
*[Security implications of using IHttpContextAccessor in Blazor Server (`dotnet/aspnetcore`#45699)](https://github.com/dotnet/aspnetcore/issues/45699)
34
+
35
+
†Most developers building and maintaining Blazor apps don't need to delve into advanced concepts as long as the general guidance in this article is followed.
15
36
16
37
:::moniker-end
17
38
@@ -24,3 +45,5 @@ The recommended approach for passing request state to the Blazor app is through
24
45
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/additional-scenarios#circuit-handler-to-capture-users-for-custom-services>.
25
46
26
47
:::moniker-end
48
+
49
+
For guidance on <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor> and <xref:Microsoft.AspNetCore.Http.HttpContext> in ASP.NET Core SignalR, see <xref:signalr/httpcontext>.
*[Build a custom version of the Authentication.MSAL JavaScript library](xref:blazor/security/webassembly/additional-scenarios#build-a-custom-version-of-the-authenticationmsal-javascript-library)
1421
1418
*[Awesome Blazor: Authentication](https://github.com/AdrienTorris/awesome-blazor#authentication) community sample links
1422
1419
*<xref:blazor/hybrid/security/index>
@@ -1438,6 +1435,7 @@ PII refers any information relating to an identified or identifiable natural per
1438
1435
*[Microsoft identity platform ID tokens](/entra/identity-platform/id-tokens)
*[Build a custom version of the Authentication.MSAL JavaScript library](xref:blazor/security/webassembly/additional-scenarios#build-a-custom-version-of-the-authenticationmsal-javascript-library)
1443
1441
*[Awesome Blazor: Authentication](https://github.com/AdrienTorris/awesome-blazor#authentication) community sample links
<xref:Microsoft.AspNetCore.Http.IHttpContextAccessor>/<xref:Microsoft.AspNetCore.Http.HttpContext> generally should be avoided with SignalR because a valid <xref:Microsoft.AspNetCore.Http.HttpContext> isn't always available. In most cases, the context doesn't exist (`null`).
16
+
17
+
Even when an <xref:Microsoft.AspNetCore.Http.HttpContext> instance is available, the context is dependent on the transport:
18
+
19
+
* WebSockets receives a single context as the result of the initial handshake.
20
+
* Long polling receives a new context per client "poll" request.
21
+
* A SignalR service receives a mocked/faked/shim context.
22
+
23
+
When working within a SignalR hub, you can access the <xref:Microsoft.AspNetCore.Http.HttpContext> directly using the <xref:Microsoft.AspNetCore.SignalR.GetHttpContextExtensions.GetHttpContext%2A?displayProperty=nameWithType> method. This method returns the <xref:Microsoft.AspNetCore.Http.HttpContext> for the current connection or `null` if the connection isn't associated with an HTTP request. This is particularly useful for retrieving HTTP connection information, such as headers and query strings, directly within the hub. We recommend calling this method over <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor> for accessing <xref:Microsoft.AspNetCore.Http.HttpContext> in the hub. For more information, see <xref:signalr/hubs#the-context-object>.
24
+
25
+
For guidance on <xref:Microsoft.AspNetCore.Http.IHttpContextAccessor>/<xref:Microsoft.AspNetCore.Http.HttpContext> in ASP.NET Core Blazor apps, see <xref:blazor/components/httpcontext>.
0 commit comments