Skip to content

Commit 7f591ac

Browse files
authored
HttpContext/IHttpContextAccessor article (#34592)
1 parent ca0d56e commit 7f591ac

File tree

8 files changed

+60
-16
lines changed

8 files changed

+60
-16
lines changed

aspnetcore/blazor/security/includes/httpcontext.md renamed to aspnetcore/blazor/components/httpcontext.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
---
2+
title: IHttpContextAccessor/HttpContext in ASP.NET Core Blazor apps
3+
author: guardrex
4+
description: Learn about IHttpContextAccessor and HttpContext in ASP.NET Core Blazor apps.
5+
monikerRange: '>= aspnetcore-3.1'
6+
ms.author: riande
7+
ms.custom: mvc
8+
ms.date: 01/30/2025
9+
uid: blazor/components/httpcontext
10+
---
11+
# `IHttpContextAccessor`/`HttpContext` in ASP.NET Core Blazor apps
12+
13+
[!INCLUDE[](~/includes/not-latest-version-without-not-supported-content.md)]
14+
115
:::moniker range=">= aspnetcore-8.0"
216

317
<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 @@
1125
public HttpContext? HttpContext { get; set; }
1226
```
1327

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&dagger;, 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+
&dagger;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.
1536

1637
:::moniker-end
1738

@@ -24,3 +45,5 @@ The recommended approach for passing request state to the Blazor app is through
2445
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>.
2546

2647
:::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>.

aspnetcore/blazor/components/index.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,10 +1865,6 @@ For more information, see the following resources:
18651865
* <xref:mvc/views/tag-helpers/builtin-th/component-tag-helper>
18661866
* <xref:blazor/components/integration>
18671867

1868-
## `IHttpContextAccessor`/`HttpContext`
1869-
1870-
[!INCLUDE[](~/blazor/security/includes/httpcontext.md)]
1871-
18721868
<!--Reference links in article-->
18731869
[1]: <xref:mvc/views/razor#code>
18741870
[2]: <xref:mvc/views/razor#using>

aspnetcore/blazor/fundamentals/signalr.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,10 +1507,6 @@ app.MapBlazorHub();
15071507
+ app.MapBlazorHub("/signalr");
15081508
```
15091509

1510-
## `IHttpContextAccessor`/`HttpContext`
1511-
1512-
[!INCLUDE[](~/blazor/security/includes/httpcontext.md)]
1513-
15141510
## Impersonation for Windows Authentication
15151511

15161512
Authenticated hub connections (<xref:Microsoft.AspNetCore.SignalR.Client.HubConnection>) are created with <xref:Microsoft.AspNetCore.Http.Connections.Client.HttpConnectionOptions.UseDefaultCredentials%2A> to indicate the use of default credentials for HTTP requests. For more information, see <xref:signalr/authn-and-authz#windows-authentication>.
@@ -1573,6 +1569,7 @@ In the preceding code, `NavManager` is a <xref:Microsoft.AspNetCore.Components.N
15731569
* <xref:blazor/security/index>
15741570
* <xref:blazor/security/interactive-server-side-rendering>
15751571
* <xref:blazor/security/additional-scenarios>
1572+
* <xref:blazor/components/httpcontext>
15761573
* [Server-side reconnection events and component lifecycle events](xref:blazor/components/lifecycle#blazor-server-reconnection-events)
15771574
* [What is Azure SignalR Service?](/azure/azure-signalr/signalr-overview)
15781575
* [Performance guide for Azure SignalR Service](/azure/azure-signalr/signalr-concept-performance)

aspnetcore/blazor/security/index.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,6 @@ The built-in or custom <xref:Microsoft.AspNetCore.Components.Authorization.Authe
108108

109109
For more information on server-side authentication, see <xref:blazor/security/index>.
110110

111-
### `IHttpContextAccessor`/`HttpContext`
112-
113-
[!INCLUDE[](~/blazor/security/includes/httpcontext.md)]
114-
115111
### Shared state
116112

117113
[!INCLUDE[](~/blazor/security/includes/shared-state.md)]
@@ -1417,6 +1413,7 @@ PII refers any information relating to an identified or identifiable natural per
14171413
* [Microsoft identity platform access tokens](/entra/identity-platform/access-tokens)
14181414
* <xref:security/index>
14191415
* <xref:security/authentication/windowsauth>
1416+
* <xref:blazor/components/httpcontext>
14201417
* [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)
14211418
* [Awesome Blazor: Authentication](https://github.com/AdrienTorris/awesome-blazor#authentication) community sample links
14221419
* <xref:blazor/hybrid/security/index>
@@ -1438,6 +1435,7 @@ PII refers any information relating to an identified or identifiable natural per
14381435
* [Microsoft identity platform ID tokens](/entra/identity-platform/id-tokens)
14391436
* [Microsoft identity platform access tokens](/entra/identity-platform/access-tokens)
14401437
* <xref:security/index>
1438+
* <xref:blazor/components/httpcontext>
14411439
* <xref:security/authentication/windowsauth>
14421440
* [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)
14431441
* [Awesome Blazor: Authentication](https://github.com/AdrienTorris/awesome-blazor#authentication) community sample links

aspnetcore/blazor/security/interactive-server-side-rendering.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ In general, we recommend that you avoid rendering components that contain sensit
6262

6363
## `IHttpContextAccessor`/`HttpContext`
6464

65-
[!INCLUDE[](~/blazor/security/includes/httpcontext.md)]
65+
For more information, see <xref:blazor/components/httpcontext>.
6666

6767
## Resource exhaustion
6868

aspnetcore/fundamentals/http-context.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public class EmailController : Controller
191191

192192
## `IHttpContextAccessor`/`HttpContext` in Razor components (Blazor)
193193

194-
[!INCLUDE[](~/blazor/security/includes/httpcontext.md)]
194+
For more information, see <xref:blazor/components/httpcontext>.
195195

196196
:::moniker-end
197197

@@ -346,6 +346,6 @@ public class EmailController : Controller
346346

347347
## `IHttpContextAccessor`/`HttpContext` in Razor components (Blazor)
348348

349-
[!INCLUDE[](~/blazor/security/includes/httpcontext.md)]
349+
For more information, see <xref:blazor/components/httpcontext>.
350350

351351
:::moniker-end

aspnetcore/signalr/httpcontext.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: IHttpContextAccessor/HttpContext in ASP.NET Core SignalR
3+
author: guardrex
4+
description: Learn about IHttpContextAccessor and HttpContext in ASP.NET Core SignalR.
5+
monikerRange: '>= aspnetcore-3.1'
6+
ms.author: riande
7+
ms.custom: mvc
8+
ms.date: 01/30/2025
9+
uid: signalr/httpcontext
10+
---
11+
# `IHttpContextAccessor`/`HttpContext` in ASP.NET Core SignalR
12+
13+
[!INCLUDE[](~/includes/not-latest-version-without-not-supported-content.md)]
14+
15+
<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>.

aspnetcore/toc.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@ items:
486486
uid: blazor/components/render-modes
487487
- name: Prerender components
488488
uid: blazor/components/prerender
489+
- name: IHttpContextAccessor and HttpContext
490+
uid: blazor/components/httpcontext
489491
- name: Generic type support
490492
uid: blazor/components/generic-type-support
491493
- name: Synchronization context
@@ -956,6 +958,9 @@ items:
956958
- name: Configuration
957959
uid: signalr/configuration
958960
displayName: signalr
961+
- name: IHttpContextAccessor and HttpContext
962+
uid: signalr/httpcontext
963+
displayName: signalr
959964
- name: Authentication and authorization
960965
uid: signalr/authn-and-authz
961966
displayName: signalr

0 commit comments

Comments
 (0)