如果用户使用Google登录,则ASP.NET Core Identity 2.0 SignoutAsync不会注销用户

Ali*_*liK 5 c# asp.net-core asp.net-core-identity asp.net-core-2.0

我有Asp.net核心身份版本2.0设置和运行.我发现,_signinManager.SignoutAsync一旦用户登录Google ,就无法注销用户.当我回到我的登录方法时,它只显示用户登录时其Claim对象仍然完好无损.

代码非常简单,如下所示

[AllowAnonymous]
public ActionResult TestGoogle()
{
    var redirectUrl = Url.Action(nameof(ExternalCallback), "Account", new { ReturnUrl = "" });
    var properties = _signInManager.ConfigureExternalAuthenticationProperties("Google", redirectUrl);
    return Challenge(properties, "Google");
}


public async Task<IActionResult> LogOff()
{
    await _signInManager.SignOutAsync();
    return RedirectToAction(nameof(HomeController.Index), "Home");
}
Run Code Online (Sandbox Code Playgroud)

McG*_*V10 15

问题是您RedirectToAction将重定向覆盖到发出的Identity Server endsession URL SignOutAsync.

至于SignOutAsync什么是过时的Authentication部分 - 从ASP.NET Core 2.0开始,它HttpContext本身就是一个扩展.

(微软的HaoK 在这里给出了相同的注销问题的相同解释.)

编辑:解决方案是AuthenticationProperties在最终的对象中发送重定向URL SignOutAsync:

// in some controller/handler, notice the "bare" Task return value
public async Task LogoutAction()
{
    // SomeOtherPage is where we redirect to after signout
    await MyCustomSignOut("/SomeOtherPage");
}

// probably in some utility service
public async Task MyCustomSignOut(string redirectUri)
{
    // inject the HttpContextAccessor to get "context"
    await context.SignOutAsync("Cookies");
    var prop = new AuthenticationProperties()
    {
        RedirectUri = redirectUri
    };
    // after signout this will redirect to your provided target
    await context.SignOutAsync("oidc", prop);
}
Run Code Online (Sandbox Code Playgroud)

  • 愿你下雨!我只能给你一个。我当时正在执行相同的SignOutAsync,然后执行RedirectToAction,并为为什么不起作用而感到悲伤。这是我的最佳解决方案。 (2认同)
  • 我收到错误 - “没有为方案‘oidc’注册注销身份验证处理程序。 (2认同)