OWIN OpenID提供程序 – GetExternalLoginInfo()返回null
我在使用ASP.NET身份的ASP.NET MVC5应用程序中遇到OWIN OpenId提供程序的问题,并且基于具有个人用户帐户身份validation的VS2013模板。 用于Google和LinkedIn的OWIN OpenID提供程序用于登录身份validation。
问题是看起来很随机; 即使登录validation成功,GetExternalLoginInfo()也会在LoginConfirmation回调中返回null。
var authManager = HttpContext.Current.GetOwinContext().Authentication; var login = authManager.GetExternalLoginInfo();
使用的提供商是Google(Microsoft.Owin.Security.Google 2.1.0)和LinkedIn(来自Owin.Security.Providers 1.3),两家提供商都会导致同样的问题。
有时它失败一次然后再次工作,但有时它会继续失败,直到AppPool被回收。
目前,该应用程序的两个实例托管在同一Windows Azure虚拟机上的IIS中。 每个实例都有自己的AppPool但设置相同(不同的子域)。 有时登录在一个实例上停止工作,但仍然在另一个实例上工作。
该问题也在本地重现(IIS Express – VS2013)。
任何人都遇到类似的OWIN OpenID身份validation问题?
Startup.Auth.cs看起来像这样:
public void ConfigureAuth(IAppBuilder app) { // Enable the application to use a cookie to store information for the signed in user app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), }); // Use a cookie to temporarily store information about a user logging in with a third party login provider app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); app.UseGoogleAuthentication(); app.UseLinkedInAuthentication("clientId", "clientSecret"); }
以下OWIN nuget包正在使用中:
缺少ASP.NET_SessionId
cookie时会发生此问题。
在重定向到OpenID提供程序以获取凭据之前在会话中设置虚拟值似乎可以解决问题:
[AllowAnonymous] public ActionResult Login(string returnUrl) { Session["dummy"] = "dummy"; // Create ASP.NET_SessionId cookie return View(); }
本答案中的更多详细信息: https : //stackoverflow.com/a/21234614/205023
对我来说,放置ControllerContext.HttpContext.Session.RemoveAll();
在AccountController和ManageController中,解决了问题:
public ActionResult ExternalLogin(string provider, string returnUrl) { ControllerContext.HttpContext.Session.RemoveAll(); // Request a redirect to the external login provider return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl })); }
和
public ActionResult LinkLogin(string provider) { ControllerContext.HttpContext.Session.RemoveAll(); // Request a redirect to the external login provider to link a login for the current user return new AccountController.ChallengeResult(provider, Url.Action("LinkLoginCallback", "Manage"), User.Identity.GetUserId()); }
接受的答案并没有为我解决问题; 通过Google Developers Console在API Manager中启用“Google+ API”的工作是什么。
我正在使用SecuritySwitch来设置安全和非安全页面,我的问题的原因是/ signin-google路径被重定向到非安全请求,而不是使用安全请求。
正确的方法是更新解决方案中的所有owin组件。
- ASP.NET Identity扩展方法以访问用户属性
- ‘AspNetUser’类型的属性’Claims’不是导航属性
- Asp.Net Identity和多种登录机制
- 在Identity Framework的方法中,HttpContext.Current为null
- 如何检查ASP.NET MVC 5中客户端是否已存在用户?
- ‘Compare’是’System.ComponentModel.DataAnnotations.CompareAttribute’和’System.Web.Mvc.CompareAttribute’之间的模糊引用。
- 通过ASP.NET身份2中的UserManager.Update()更新用户
- 网站初始化时创建默认用户/管理员 – 身份
- InvalidOperationException:无法为“Role”创建DbSet,因为此类型未包含在上下文的模型中