Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在blazor服务器端注销?_C#_Blazor - Fatal编程技术网

C# 如何在blazor服务器端注销?

C# 如何在blazor服务器端注销?,c#,blazor,C#,Blazor,我在blazor中使用HttpCOntextAccessor注销时遇到问题 我试图注销,但什么也没做。我想要的只是删除浏览器中的一些cookie,我将重定向到hompage并再次登录,我想如果我只使用httpcontext注销,我可以自动删除cookie,因为我正在注销 这是注销代码 @page "/" @* @inject IJSRuntime JSRuntime *@ @* @inherits FragmentNavigationBas

我在blazor中使用HttpCOntextAccessor注销时遇到问题 我试图注销,但什么也没做。我想要的只是删除浏览器中的一些cookie,我将重定向到hompage并再次登录,我想如果我只使用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("/");
          }

      }
@page/“
@*@inject IJSRuntime JSRuntime*@
@*@继承FragmentNavigationBase*@
@使用System.Security.Claims
@注入Microsoft.AspNetCore.Http.IHttpContextAccessor\u httpContextAccessor
@注入NavigationManager NavigationManager
@使用制度;
@使用System.Threading.Tasks;
@使用Microsoft.AspNetCore.Authentication;
@使用Microsoft.AspNetCore.Authentication.Cookies;
@注入Blazored.LocalStorage.ILocalStorageService LocalStorage
订票人员
家
报告
报告组
报告室
报告时间表
Hai,@EmployeeName注销
@代码{
公共字符串EmployeeName{get;set;}
公共字符串电子邮件{get;set;}
公共字符串Nik{get;set;}
公共字符串NikLama{get;set;}
受保护的覆盖无效OnInitialized()
{
var name=\u 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;;
控制台写入线(测试);
}
受保护的void OnLogOut()
{
_httpContextAccessor.HttpContext.SignOutAsync(
CookieAuthenticationDefaults.AuthenticationScheme);
NavigationManager.NavigateTo(“/”);
}
}

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

Blazor服务器使用Signal,这意味着在
启动之后。Configure
已经运行了它的过程,即从您的组件运行

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

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

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

public void配置(IApplicationBuilder应用程序){
// ...
应用程序使用(异步(上下文,下一步)=>{
//^^^^^^^^^^ HttpContext
if(context.Request.Path)
.Equals(“/Logout”,System.StringComparison.OrdinalIgnoreCase))
{
//删除cookie、重定向到登录和任何其他注销逻辑
}
等待下一个();
});
// ...
}

如果你走这条路线,考虑一下做一个代替。

可能是那个信号,表明你出了代码> AsYNC/<代码>?因此,在注销实际发生之前,您将导航离开。您需要等待它。我输入了一个异步等待。并且得到了这些错误
未处理的异常呈现组件:头是只读的,响应已经开始。
什么意思?我不能在信号器中使用httpcontext?所以我不能使用signoutasync?所以基本上你说我不能在http上下文上使用signoutasync?那我怎么才能让blazor退出呢?我没有使用identity manager,我需要的只是删除Cookies我认为这与此不同,因为当我试图使其注销时,它不起作用