gpt4 book ai didi

c# - 如何在 Blazor 服务器端注销?

转载 作者:行者123 更新时间:2023-12-02 02:12:03 26 4
gpt4 key购买 nike

我在使用 HttpContextAccessor 在 Blazor 中编程注销时遇到问题。

我尝试注销,但没有执行任何操作。我想要的只是删除浏览器中的一些 cookie 并重定向到主页,以便我可以再次进入登录页面;我认为通过使用 httpcontext 注销,我可以自动删除 cookie,因为我已注销。

这是注销代码:

      @page "/"
@* @inject IJSRuntime JSRuntime *@
@* @inherits FragmentNavigationBase *@
@using System.Security.Claims
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor _httpContextAccessor
@inject NavigationManager NavigationManager
@using System;
@using System.Threading.Tasks;
@using Microsoft.AspNetCore.Authentication;
@using Microsoft.AspNetCore.Authentication.Cookies;
@inject Blazored.LocalStorage.ILocalStorageService localStorage



<Layout>
<div class="container">
<Bar
Breakpoint="Breakpoint.Desktop"
Background="Background.Light"
ThemeContrast="ThemeContrast.Light"
>
<div>
Booking Crew
</div>
<BarToggler />
<BarMenu>
<BarStart>
<BarItem>
<BarLink To="">Home</BarLink>
</BarItem>
<BarItem>
<BarDropdown>
<BarDropdownToggle>Report</BarDropdownToggle>
<BarDropdownMenu>
<BarDropdownItem><BarLink To="report_crews">Report Crew</BarLink></BarDropdownItem>
<BarDropdownItem><BarLink To="report_studio">Report Studio</BarLink></BarDropdownItem>
<BarDropdownItem><BarLink To="report_schedule">Report Schedule</BarLink></BarDropdownItem>
</BarDropdownMenu>
</BarDropdown>
</BarItem>
</BarStart>
</BarMenu>
Hai, @EmployeeName <span style="cursor: pointer;" @onclick="OnLogOut"> Logout</span>
</Bar>
</div>

</Layout>

@code{
public string EmployeeName {get;set;}
public string Email {get;set;}
public string Nik {get;set;}
public string NikLama {get;set;}
protected override void OnInitialized()
{
var name = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Name).Value;
name = name.ToString().ToLower();
EmployeeName = name.Remove(1).ToUpper() + name.Substring(1);
Email = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.Email).Value;
Nik = _httpContextAccessor.HttpContext.User.FindFirst(c => c.Type == "urn:T7SSO:nik")?.Value;
NikLama = _httpContextAccessor.HttpContext.User.FindFirst(c => c.Type == "urn:T7SSO:nik_lama")?.Value;
var test = _httpContextAccessor.HttpContext.User.FindFirst(c => c.Type == "urn:T7SSO:nik")?.Value;;
Console.WriteLine(test);
}

protected void OnLogOut()
{
_httpContextAccessor.HttpContext.SignOutAsync(
CookieAuthenticationDefaults.AuthenticationScheme);
NavigationManager.NavigateTo("/");
}

}

我认为使用 signoutAsync 足以使用户注销,但是当我单击注销时,什么也没有发生。我该如何解决这个问题?

最佳答案

Blazor Server 使用 SignalR,这意味着在 Startup.Configure 运行完毕后,you can no longer safely access the HttpContext ,即来自您的组件。

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.

如果您需要删除 cookie,请导航到可由 HttpContext 有效的代码(例如 Controller 或中间件)处理的注销 URL。

如果您只需要简单的注销功能,请使用中间件并避免添加对 AddControllersMapControllers 的调用。这是使用内联中间件的示例:

public void Configure(IApplicationBuilder app) {
// ...
app.Use(async (context, next) => {
// ^^^^^^^ the HttpContext
if (context.Request.Path
.Equals("/Logout", System.StringComparison.OrdinalIgnoreCase))
{
// Remove cookie, redirect to landing, and any other logout logic
}
await next();
});
// ...
}

如果您走这条路,请考虑制作 custom middleware class相反。

关于c# - 如何在 Blazor 服务器端注销?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67616358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com