Google身份验证工作不稳定。
Google身份验证工作不稳定。
我在我的.NET网页应用程序中使用Google OAuth作为身份验证模式。尽管在我的机器上似乎工作正常,但在实际环境中似乎工作不稳定。
在开发者控制台中输入的详细信息如下:
并且已启用Google+ API:
默认的ExternalLogin
方法如下:
public ActionResult ExternalLogin(string provider, string returnUrl) { return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl })); }
而ExternalLoginCallback
定义如下:
public async TaskExternalLoginCallback(string returnUrl) { var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { return RedirectToAction("Login"); } var userEmail = loginInfo.Email; var loggedInUser = VerifyAndAuthenticateUser(userEmail); if (loggedInUser != null) { FormsAuthentication.SetAuthCookie(userEmail, false); return RedirectToLocal(returnUrl); } return RedirectToAction("login", "account"); }
Google提供商的ID和密钥填写在Startup.Auth.cs
文件中:
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions() { ClientId = "xxxx", ClientSecret = "xxxx" });
我的web.config
文件在system.web元素中包含Forms身份验证:
我在ExternalLogin
和ExternalLoginCallback
方法中添加了几行代码来记录失败的位置,似乎ExternalLoginCallback
无法被调用。同样,这种情况是间歇性的,有时我能够完成登录。问题可能是什么?
Google身份验证出现间歇性工作的问题已经通过应用来自引用的SO答案中的以下两个更改解决:
在Startup.Auth
中进行更改(OWIN的GetExternalLoginInfoAsync始终返回null):
var google = new GoogleOAuth2AuthenticationOptions { ClientId = "ClientId", ClientSecret = "ClientSecret", Provider = new GoogleOAuth2AuthenticationProvider() }; google.Scope.Add("email"); app.UseGoogleAuthentication(google);
在AccountController
中进行更改(MVC5 Facebook登录时出现空引用):
public ActionResult ExternalLogin(string provider, string returnUrl) { ControllerContext.HttpContext.Session.RemoveAll(); var redirectUri = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }); return new ChallengeResult(provider, redirectUri); }