AuthenticationManager.GetExternalLoginInfo除了在ExternalLoginCallback返回null [英] AuthenticationManager.GetExternalLoginInfo returns null except in ExternalLoginCallback

查看:275
本文介绍了AuthenticationManager.GetExternalLoginInfo除了在ExternalLoginCallback返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VS2013的标准模板MVC并启用外部登录。它工作正常,而我现在增加了一些功能。

I am using the standard MVC template in VS2013 and have enabled external logins. It works fine, and I am now adding some features.

我觉得 AuthenticationManager.GetExternalLoginInfo 在ExternalLoginCallback行动工作正常。但是,如果我有任何其他地方,它总是返回null,即使用户登录我加入的账户控制器下面做了一个试验:

I find that AuthenticationManager.GetExternalLoginInfo works fine in the ExternalLoginCallback action. However, if I have it anywhere else, it always returns null, even if the user is logged in. I did a test by adding the following in the Account controller:

    [Authorize]
    public async Task<ActionResult> Test()
    {
        Debug.Assert(User.Identity.IsAuthenticated);
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        ViewBag.Provider = loginInfo.Login.LoginProvider;
        return View();
    }

账户控制器需要授权,但我加了属性的授权只是为了以防万一。我试图与Twitter和谷歌帐户(用户必须成功登录到执行该code),GetExernalLoginInfo总是会在上述方法返回null。我只有外部账户,并有与本地密码没有帐号,这样就没有机会,一个本地用户是偶然登录。

The Account controller requires authorization but I added the Authorize attribute just to be doubly sure. I tried with both Twitter and Google accounts (user has to be successfully logged in to execute this code), GetExernalLoginInfo will always return null in the above method. I have only external accounts, and there are no accounts with local passwords so there is no chance that a local user was logged in by accident.

为什么它不工作?不GetExternalLoginInfo从饼干得出的结果?

Why does it not work? Doesn't GetExternalLoginInfo derive the result from the cookies?

编辑:从运用 UseKentorOwinCookieSaver 补丁<一个href=\"http://stackoverflow.com/questions/20737578/asp-net-sessionid-owin-cookies-do-not-send-to-browser\">ASP.NET_SessionId + OWIN Cookie不会发送到浏览器没有帮助的。

Edits: Applying the UseKentorOwinCookieSaver patch from ASP.NET_SessionId + OWIN Cookies do not send to browser didn't help either.

推荐答案

这是因为在MVC模板使用SignInManager类。正因为如此,默认情况下所有的用户都局部签订,也与外部帐户登录后。

It happens because the MVC template uses the SignInManager class. Because of that, by default all users are signed in locally, also after logging in with an external account.

在ExternalLoginCallback方法有以下行:

In the ExternalLoginCallback method there is the following line:

var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);

这签署了外部用户信息,并与一个ApplicationCookie本地用户的迹象。
如果执行此行之后看看AuthenticationManager的,你会看到一个AuthenticationResponseGrant.AuthentityType = ApplicationCookie和AuthenticationResponseRevoke.AuthenticationType = ExternalCookie。

This signs out the external userinfo, and signs in the local user with an ApplicationCookie. If you take a look at the AuthenticationManager after this line is executed, you will see an AuthenticationResponseGrant.AuthentityType=ApplicationCookie and AuthenticationResponseRevoke.AuthenticationType=ExternalCookie.

最终,ExternalCookie是当Owin中间件检查的上下文中除去。
这样AuthenticationManager.GetExternalLoginInfo()登录时,cookie持有信息已被删除,并由ApplicationCookie更换后返回null。

Eventually the ExternalCookie is removed when the Owin middleware inspects the context. That way AuthenticationManager.GetExternalLoginInfo() returns null after logging in, the cookie holding the info has been removed and replaced by a ApplicationCookie.

这篇关于AuthenticationManager.GetExternalLoginInfo除了在ExternalLoginCallback返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆