如何在 Blazor 服务器端注销?

App*_*pem 2 c# blazor

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

我尝试注销,但没有执行任何操作。我想要的只是删除浏览器中的一些 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("/");
          }

      }
Run Code Online (Sandbox Code Playgroud)

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

Con*_*Low 5

Blazor Server 使用 SignalR,这意味着在Startup.Configure运行完毕后,您无法再HttpContext从组件安全地访问 , ie 。

此外,出于安全原因,您不得在 Blazor 应用中使用 IHttpContextAccessor。Blazor 应用程序在 ASP.NET Core 管道的上下文之外运行。不保证 HttpContext 在 IHttpContextAccessor 中可用,也不保证保存启动 Blazor 应用程序的上下文。

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

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

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();
    });
    // ...
}
Run Code Online (Sandbox Code Playgroud)

如果您走这条路,请考虑制作一个自定义中间件类