2017-09-09 66 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