为什么GetExternalLoginInfoAsync()在这种情况下返回null?

时间:2017-12-07 18:44:03

标签: c# oauth-2.0 owin asp.net-identity-2 google-authentication

我使用Identity2 MVC5和Google登录创建了一个应用程序,以便向Google执行SSO ...

如果我成功登录,请关闭浏览器,然后重新登录, 此代码块获取loginInfo == null,

public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
        {
            var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
            if (loginInfo == null)
            {
                return RedirectToAction( "LogOut" );
                //return RedirectToAction("Login");
            }

我最好的猜测是asp.net cookie是会话并因此被删除,但Google Oauth令牌仍然在某个地方挥之不去......

这是怎么回事? 我该如何清理oauth令牌?

我尝试了几种方法,在登录页面的PageReady上运行(即在登录页面时清理所有持久登录)

var user = UserManager.FindByName( User.Identity.Name );
var AuthenticationManager = HttpContext.GetOwinContext().Authentication;

AuthenticationManager.SignOut();
AuthenticationManager.SignOut( DefaultAuthenticationTypes.ApplicationCookie );
Session.Abandon();

if ( user != null )
{
    UserManager.UpdateSecurityStamp( user.Id ); // remove the old cookie so it cant' be reused to re-log in - EWB
}

最后

    return Redirect( "https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=https://"+ Url.Action( "Index", "Home", new { target = "_blank" } ) ); //https://stackoverflow.com/questions/27515518/asp-net-identity-external-login-wont-log-out - f belihocine answer

应该退出谷歌(并转到我们的自定义注销页面,所以它似乎是真的)。​​

但我仍然有同样的行为

到底发生了什么?

如果我等到超时到期(10分钟),事情就会正常工作......

1 个答案:

答案 0 :(得分:0)

它返回null,因为我创建了一个app cookie和一个外部cookie,并且只删除了app cookie。

以我想要的方式调用此方法

def index(request):
    #request.session.flush()
    if request.user.is_authenticated:
        return redirect('ve:dashboard')
    elif request.method == 'POST':
        form = RegistrationForm(request.POST)

        if form.is_valid():
            user = form.save(commit=False)
            # do anything you need to the unsaved user here
            user.save()
            prof = Profile.objects.create(user=user, 
                                          ipaddress=get_ip(request),
                                          date=form.cleaned_data.get('birth_date')
            # no need to save prof since we called objects.create
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=user.username, password=raw_password)
            login(request, user)
            return redirect('ve:dashboard')
    else:
        form = RegistrationForm()
    return render(request, 'index.html', {'form': form})