简体   繁体   English

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

[英]ASP.NET Core Identity 2.0 SignoutAsync is not logging out user if the user signed in with Google

I have Asp.net Core Identity version 2.0 Set up and running. 我已经安装并运行了Asp.net Core Identity 2.0版。 I am finding that _signinManager.SignoutAsync is not logging out user once they have signed in with Google. 我发现_signinManager.SignoutAsync在用户使用Google登录后并未注销。 When I go back to my Login Method it just shows the User as logged in with their Claims object still intact. 当我返回到登录方法时,它仅显示用户已登录,而其Claims对象仍保持不变。

The code is really simple as below 代码非常简单,如下所示

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

The problem is that your RedirectToAction overwrites the redirect to the Identity Server endsession URL that SignOutAsync issues. 问题是您的RedirectToAction覆盖对SignOutAsync发出的Identity Server结束会话URL的重定向。

As for SignOutAsync , what is obsolete is the Authentication portion -- as of ASP.NET Core 2.0 it's an extension directly off HttpContext itself. 至于SignOutAsync ,过时的是Authentication部分-从ASP.NET Core 2.0开始,它是HttpContext本身的直接扩展。

(The same explanation for the same signout problem is given here by Microsoft's HaoK.) (微软的HaoK 在这里给出了相同退出问题的相同解释。)

Edit: The solution is to send a redirect URL in an AuthenticationProperties object with the final SignOutAsync : 编辑:解决方案是使用最终的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);
}

Just the first line solved my issue. 仅第一行解决了我的问题。

await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await _SignInManager.SignOutAsync();
HttpContext.Response.Cookies.Delete(".AspNetCore.Cookies");

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

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