如何在服务器端Blazor中访问HttpContext?

Ale*_*tov 5 cookies httpcontext blazor

我需要访问HttpContext页面(.cshtml),尤其是访问请求和Cookie。尽管可用,但 HttpContextAccessor始终在其属性中存储一个HttpContext

任何想法将不胜感激。

提前致谢。

编辑:我使用的Blazor版本是:0.7.0。

Isa*_*aac 11

将以下内容添加到Blazor.Web.App.Startup.cs:

services.AddHttpContextAccessor();
Run Code Online (Sandbox Code Playgroud)

您也需要 <component-name>.cshtml

@using Microsoft.AspNetCore.Http
@inject IHttpContextAccessor httpContextAccessor
Run Code Online (Sandbox Code Playgroud)

  • 请注意,Microsoft 不推荐“IHttpContextAccessor”:https://github.com/dotnet/aspnetcore/issues/17585#issuecomment-561715797 (5认同)
  • 您不应在服务器端 blazor 上使用 IHttpContextAccessor。您应该将值从 cshtml 传递给应用程序。请检查此代码示例 https://github.com/wmgdev/BlazorGraphApi (3认同)

小智 11

请注意,文档明确指出 IHttpContextAccessor 不应用于 Blazor 应用程序: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-context ?view=aspnetcore-3.1#blazor-and -共享状态

(这并不意味着在您的特定情况下它不适用于特定场景。但无论如何您只能从第一个请求中获取 cookie - 一旦 SignalR 接管就不再有 cookie - 您也许应该得到当 _Host.cshtml 为 renderend 时的值,并将其作为字符串值从那里传递给 Blazor 组件。)


Shi*_*mmy 7

这取决于你想访问什么HttpContext

如果您想访问身份验证或用户信息,请考虑使用AuthenticationStateProvider

@page "/"
@using System.Security.Claims
@using Microsoft.AspNetCore.Components.Authorization
@inject AuthenticationStateProvider AuthenticationStateProvider

<h3>ClaimsPrincipal Data</h3>

<button @onclick="GetClaimsPrincipalData">Get ClaimsPrincipal Data</button>

<p>@_authMessage</p>

@if (_claims.Count() > 0)
{
    <ul>
        @foreach (var claim in _claims)
        {
            <li>@claim.Type &ndash; @claim.Value</li>
        }
    </ul>
}

<p>@_surnameMessage</p>

@code {
    private string _authMessage;
    private string _surnameMessage;
    private IEnumerable<Claim> _claims = Enumerable.Empty<Claim>();

    private async Task GetClaimsPrincipalData()
    {
        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        var user = authState.User;

        if (user.Identity.IsAuthenticated)
        {
            _authMessage = $"{user.Identity.Name} is authenticated.";
            _claims = user.Claims;
            _surnameMessage = 
                $"Surname: {user.FindFirst(c => c.Type == ClaimTypes.Surname)?.Value}";
        }
        else
        {
            _authMessage = "The user is NOT authenticated.";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这应该标记为答案。我也遇到过这个问题,发现 HttpContextAccessor 不起作用,但可以使用 AuthenticationStateProvider: var authState = wait AuthenticationStateProvider.GetAuthenticationStateAsync(); var currUserClaimsPrincipal = authState.User; var currUser = 等待 UserManager.GetUserAsync(currUserClaimsPrincipal); (2认同)

Flo*_*res 5

blazor.Sever 到 Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddServerSideBlazor<Client.Startup>();

    // HttpContextAccessor
    services.AddHttpContextAccessor();
    services.AddScoped<HttpContextAccessor>();
}
Run Code Online (Sandbox Code Playgroud)

blazor.Shared

public class HttpContextAccessor
{
   private readonly IHttpContextAccessor _httpContextAccessor;

   public HttpContextAccessor(IHttpContextAccessor httpContextAccessor)
   {
        _httpContextAccessor = httpContextAccessor;
   }

   public HttpContext Context => _httpContextAccessor.HttpContext;
}
Run Code Online (Sandbox Code Playgroud)

blazor.Client 到 App.cshtml

@inject blazor.Shared.HttpContextAccessor HttpContext
<Router AppAssembly=typeof(Program).Assembly />

@functions 
{      
    protected override void OnInit()
    {
       HttpContext.Context.Request.Cookies.**

       // Or data passed through middleware in blazor.Server
       HttpContext.Context.Features.Get<T>()
    }
}
Run Code Online (Sandbox Code Playgroud)

学分:https : //github.com/aspnet/Blazor/issues/1554

  • 请注意,Microsoft 不推荐“IHttpContextAccessor”:https://github.com/dotnet/aspnetcore/issues/17585 (6认同)
  • 同志,这个解决办法是错误的,行不通的。顺便说一句,HttpContextAccessor 类是在 Microsoft.AspNetCore.Http 命名空间中定义的,不应重新定义。该类实现 IHttpContextAccessor 接口。AddHttpContextAccessor 为您创建一个单例对象。所以使用它。 (3认同)