我在VS2013中使用标准MVC模板并启用了外部登录。它工作正常,我现在正在添加一些功能。
我发现AuthenticationManager.GetExternalLoginInfo
在ExternalLoginCallback操作中工作正常。但是,如果我在其他任何地方都有它,它总是返回null,即使用户已登录。我通过在帐户控制器中添加以下内容进行了测试:
[Authorize]
public async Task<ActionResult> Test()
{
Debug.Assert(User.Identity.IsAuthenticated);
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
ViewBag.Provider = loginInfo.Login.LoginProvider;
return View();
}
帐户控制器需要授权,但我添加了Authorize属性只是为了更加确定。我尝试使用Twitter和Google帐户(用户必须成功登录才能执行此代码),GetExernalLoginInfo将始终在上述方法中返回null。我只有外部帐户,并且没有使用本地密码的帐户,因此本地用户无法偶然登录。
为什么不起作用? GetExternalLoginInfo不会从cookie中获得结果吗?
编辑:应用ASP.NET_SessionId + OWIN Cookies do not send to browser中的UseKentorOwinCookieSaver
补丁也无济于事。
答案 0 :(得分:1)
这是因为MVC模板使用SignInManager类。因此,默认情况下,所有用户都在本地登录,也是在使用外部帐户登录后。
在ExternalLoginCallback方法中有以下行:
var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false);
这将注销外部userinfo,并使用ApplicationCookie在本地用户中签名。 如果您在执行此行后查看AuthenticationManager,您将看到AuthenticationResponseGrant.AuthentityType = ApplicationCookie和AuthenticationResponseRevoke.AuthenticationType = ExternalCookie。
当Owin中间件检查上下文时,最终会删除ExternalCookie。 这样,AuthenticationManager.GetExternalLoginInfo()在登录后返回null,保存信息的cookie已被删除并被ApplicationCookie取代。