web-dev-qa-db-ja.com

ユーザーがGoogleでサインインした場合、ASP.NET Core Identity 2.0SignoutAsyncはユーザーをログアウトしません

Asp.net CoreIdentityバージョン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");
}
6
AliK

問題は、RedirectToActionSignOutAsyncが発行するIdentityServerエンドセッションURLへのリダイレクトを上書きすることです。

SignOutAsyncに関しては、廃止されたのはAuthentication部分です。ASP.NETCore2.0の時点では、HttpContext自体から直接拡張されています。

(同じサインアウトの問題について同じ説明が与えられています ここ MicrosoftのHaoKによって。)

編集:解決策は、リダイレクトURLをAuthenticationPropertiesオブジェクトで最後の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);
}
18
McGuireV10

最初の行だけで私の問題は解決しました。

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