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)
小智 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 组件。)
这取决于你想访问什么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 – @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)
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
归档时间: |
|
查看次数: |
2499 次 |
最近记录: |