[英]ASP.NET Core Identity 2.0 SignoutAsync is not logging out user if the user signed in with Google
我已经安装并运行了Asp.net Core Identity 2.0版。 我发现_signinManager.SignoutAsync
在用户使用Google登录后并未注销。 当我返回到登录方法时,它仅显示用户已登录,而其Claims对象仍保持不变。
代码非常简单,如下所示
[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结束会话URL的重定向。
至于SignOutAsync
,过时的是Authentication
部分-从ASP.NET Core 2.0开始,它是HttpContext
本身的直接扩展。
(微软的HaoK 在这里给出了相同退出问题的相同解释。)
编辑:解决方案是使用最终的SignOutAsync
在AuthenticationProperties
对象中发送重定向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);
}
仅第一行解决了我的问题。
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await _SignInManager.SignOutAsync();
HttpContext.Response.Cookies.Delete(".AspNetCore.Cookies");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.