Google身份验证工作不稳定。

9 浏览
0 Comments

Google身份验证工作不稳定。

我在我的.NET网页应用程序中使用Google OAuth作为身份验证模式。尽管在我的机器上似乎工作正常,但在实际环境中似乎工作不稳定。

在开发者控制台中输入的详细信息如下:

Google Developer Console

并且已启用Google+ API:

Google+

默认的ExternalLogin方法如下:

public ActionResult ExternalLogin(string provider, string returnUrl)
{
    return new ChallengeResult(provider,
        Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
}

ExternalLoginCallback定义如下:

public async Task ExternalLoginCallback(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身份验证:

我在ExternalLoginExternalLoginCallback方法中添加了几行代码来记录失败的位置,似乎ExternalLoginCallback无法被调用。同样,这种情况是间歇性的,有时我能够完成登录。问题可能是什么?

0
0 Comments

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);
}

0