2017-09-09 158 views
0

我有Asp.net Core Identity版本2.0設置和運行。我發現_signinManager.SignoutAsync在用戶登錄Google後沒有註銷。當我回到我的登錄方法時,它只是顯示用戶登錄時其聲明對象仍然完好無損。ASP.NET Core Identity 2.0 SignoutAsync

的代碼如下

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

嗨,也許是餅乾。嘗試在您註冊: await HttpContext.Authentication.SignOutAsync(「yourcookie」); –

+0

看着這個答案,但VS說,該方法已過時,將在未來版本中刪除。所以想避免。 – AliK

回答

0

非常簡單的問題是,你RedirectToAction覆蓋重定向到身份服務器endsession網址SignOutAsync問題。

至於SignOutAsync,已過時的部分是Authentication部分 - 從ASP.NET Core 2.0開始,它直接是HttpContext本身的擴展。

(爲同一signout問題同樣的解釋是由微軟的HaoK給出here

編輯:解決的辦法是發送重定向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); 
} 
+0

誰downvoted,爲什麼?我有這個代碼在生產中運行,並且我遇到了同樣的問題。我確定這是正確的,因爲我可以提供問題提供的細節。看起來像是對我而言的一個驅動器。 – McGuireV10

+1

可能會升起你的雨!我只能給你一個。我正在做同樣的SignOutAsync然後RedirectToAction,併爲什麼它不起作用感到悲傷。這是我的完美修復。 – ilikeprogramming

相關問題