繁体   English   中英

如何在 blazor 服务器端注销?

[英]how to logout on blazor server side?

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

我试图注销,但它没有做任何事情。 我想要的只是在我的浏览器中删除一些 cookies 并重定向到主页,这样我就可以和 go 再次到登录页面; 我认为通过使用 httpcontext 注销我可以自动删除 cookies 因为我已注销。

这是注销代码:

      @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 服务器使用 SignalR,这意味着在Startup.Configure运行后, 您不能再安全地访问HttpContext ,即从您的组件。

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

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

如果您只需要简单的注销功能,请使用中间件并避免添加对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();
    });
    // ...
}

如果您使用 go 这条路线,请考虑制作自定义中间件 class代替。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM