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

时间:2018-12-17 14:35:52

标签: cookies httpcontext blazor

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

任何想法都会受到赞赏。

谢谢。

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

5 个答案:

答案 0 :(得分:4)

这取决于您要访问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.";
        }
    }
}

答案 1 :(得分:2)

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

(这并不意味着在您的特定情况下,它不适用于特定的情况。但是无论如何,您只能从第一个请求中获取cookie-一旦SignalR接管了no cookie,您应该可能会在_Host.cshtml渲染结束时获取该值,然后将其作为字符串值从那里传递给Blazor组件。)

答案 2 :(得分:1)

blazor.Sever到Startup.cs

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

    // HttpContextAccessor
    services.AddHttpContextAccessor();
    services.AddScoped<HttpContextAccessor>();
}

blazor.Shared

public class HttpContextAccessor
{
   private readonly IHttpContextAccessor _httpContextAccessor;

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

   public HttpContext Context => _httpContextAccessor.HttpContext;
}

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>()
    }
}

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

答案 3 :(得分:1)

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

services.AddHttpContextAccessor();

您还需要<component-name>.cshtml

@using Microsoft.AspNetCore.Http
@inject IHttpContextAccessor httpContextAccessor

答案 4 :(得分:0)

按照此处的 Microsoft 说明进行操作后

https://docs.microsoft.com/en-us/aspnet/core/migration/31-to-50?view=aspnetcore-5.0&tabs=visual-studio

为了将我的项目升级到 asp.net core 5.0,我导致了这个问题。

第 6 步说用 addtransient 替换 addscoped。没有 addtransient 并且刚刚开始我的 blazor 之旅,我猜测并将 addscoped 行添加到我的代码中。这会阻止令牌与用户一起传递。

我认为正确的方法是仅在替换 addTransient 行时才添加此行

builder.Services.AddScoped(sp => new HttpClient{ BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });