【问题标题】:Google Authentication working intermittentlyGoogle 身份验证间歇性工作
【发布时间】:2016-12-31 21:18:30
【问题描述】:

我正在使用 Google OAuth 作为我的 .NET Web 应用程序的身份验证模式。虽然它似乎在我的机器上运行良好,但在实时环境中它似乎间歇性地运行。

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

并且 Google+ API 已设置为启用:

默认ExternalLogin方法如下:

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

而ExternalLoginCallback的定义如下:

public async Task<ActionResult> 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");
}

并且在Startup.Auth.cs文件中填写了Google provider Id和Secret:

app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
    ClientId = "xxxx",
    ClientSecret = "xxxx"
});

我的web.config 在 system.web 元素中包含表单身份验证:

<authentication mode="Forms">
  <forms loginUrl="~/account/login" timeout="2880" />
</authentication>

我在ExternalLogin 和ExternalLoginCallback 方法中添加了几行代码来记录失败的地方,似乎ExternalLoginCallback 调用失败。同样,这会间歇性地发生,因为有时我能够完成我的登录。可能是什么问题?

【问题讨论】:

    标签: c# asp.net-mvc oauth google-authentication


    【解决方案1】:

    通过应用引用的 SO 答案中的以下两个更改解决了该问题:

    更改Startup.Auth (OWIN's GetExternalLoginInfoAsync Always Returns null)

    var google = new GoogleOAuth2AuthenticationOptions
    {
        ClientId = "ClientId",
        ClientSecret = "ClientSecret",
        Provider = new GoogleOAuth2AuthenticationProvider()
    };
    google.Scope.Add("email");
    app.UseGoogleAuthentication(google);
    

    更改AccountController (MVC5 Null Reference with facebook login)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-07
      • 2020-10-13
      • 2019-04-17
      • 2018-02-05
      • 1970-01-01
      • 2018-11-11
      • 2022-12-18
      相关资源
      最近更新 更多