gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:13:27 24 4
gpt4 key购买 nike

我已设置并运行 Asp.net Core Identity 2.0 版。我发现 _signinManager.SignoutAsync 在用户登录 Google 后不会注销用户。当我回到我的登录方法时,它只显示用户登录时他们的声明对象仍然完好无损。

代码很简单,如下所示

[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");
}

最佳答案

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

至于 SignOutAsync,过时的是 Authentication 部分——从 ASP.NET Core 2.0 开始,它是直接来自 HttpContext 的扩展本身。

(微软的HaoK对同样的注销问题给出了相同的解释here。)

编辑:解决方案是在带有最终SignOutAsyncAuthenticationProperties 对象中发送重定向URL:

// 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);
}

关于c# - 如果用户使用 Google 登录,ASP.NET Core Identity 2.0 SignoutAsync 不会注销用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46131517/

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