Description
(I originally posted this question on the Docs repository, but I was told to post it here instead.)
The documentation note the following on IHttpContextAccessor
:
Additionally, again for security reasons, you must not use IHttpContextAccessor within Blazor apps. Blazor apps run outside of the context of the ASP.NET Core pipeline. The HttpContext isn't guaranteed to be available within the IHttpContextAccessor, nor is it guaranteed to be holding the context that started the Blazor app.
The default HttpContextAccessor
is implemented using AsyncLocal
:
I fail to understand the security implications of using IHttpContextAccessor
in Blazor Server. AsyncLocal
in general must flow with the user's SignalR connection†. So that would also be the case for the HttpContext
stored in this AsyncLocal
. This is also what we're seeing in our tests.
- Under what situations would
IHttpContextAccessor
return a differentHttpContext
than the one that started the Blazor app? (i.e. another user) - How is this use of
AsyncLocal
any different from other uses ofAsyncLocal
and (apparently) a security consideration? - How can we pass the originating
HttpContext
from the "context of the ASP.NET Core pipeline" safely to the Blazor Server app?
† If that isn't the case, then all bets are off.