google aspnet mvc5上的AuthenticationManager.GetExternalLoginInfoAsync()返回null

Mar*_*arc 6 asp.net-mvc google-authentication owin visual-studio-2015

我使用默认的Visual Studio 2015模板和Google身份验证开发了ASPNET MVC 5应用程序.一切都在开发环境中正常工作,但实际上外部认证后的调用AuthenticationManager.GetExternalLoginInfoAsync()有时会返回null.

通常它在当天的中央时间(从08:00到20:00)返回null,但我没有找到模式,因为有时候工作.我看过开发者控制台,但是没有很多请求(过去12小时内有22个)并且都是成功的.

我尝试过其他StackOverflow线程的一些解决方案,但它们不起作用.此外,我只能在晚上尝试它们,因为这是一个个人项目,然后连接成功,我无法重现这个问题.

代码是标准的:

  • 在启动时

    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context, user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
    
        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        // Configure the sign in cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });            
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    
        // Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
        app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
    
        // Enables the application to remember the second login verification factor such as phone or email.
        // Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
        // This is similar to the RememberMe option when you log in.
        app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
    
        var google = new GoogleOAuth2AuthenticationOptions()
        {
            ClientId = "xxxx",
            ClientSecret = "xxxx",
            Provider = new GoogleOAuth2AuthenticationProvider()
        };
        google.Scope.Add("email");
        app.UseGoogleAuthentication(google);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 在ExternalLoginCallback上

    //
    // GET: /Account/ExternalLoginCallback
    [AllowAnonymous]
    public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {
        Log.Debug("AuthenticationManager.GetExternalLoginInfoAsync()");
        var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        if (loginInfo == null)
        {
            Log.Error("AuthenticationManager.GetExternalLoginInfoAsync(): null");
            return RedirectToAction("Login");
        }
    ...
    
    Run Code Online (Sandbox Code Playgroud)

更多信息
我已经与另一个用户创建了新的Google凭据,当我更改clientId和clientSecret时,它再次有效......我甚至不知道...

更多信息
问题不在于凭证,我"只"需要重启ASP.NET应用程序来解决问题,也许这个新线索可以帮助有人帮助我.

没有复制
我发布了答案,并不是因为OWIN的GetExternalLoginInfoAsync始终返回null帖子,我在那里找到了我找到解决方案的线程:ASP.NET_SessionId + OWIN Cookies不发送到浏览器

Mar*_*arc 11

最后(我认为)我在一周之后找到了解决方案,没有登录失败.一切都归功于StackOverflow线程.我的解决方案是在AccountController.ExternalLogin行动中插入以下行:

Session["Workaround"] = 0;
Run Code Online (Sandbox Code Playgroud)

在上面的线程(以及那里提供的链接)中,在混合ASPNET MVC和OWIN组件的会话和cookie时,可以更好地解释该错误.

完整控制器服务代码:

    //
    // POST: /Account/ExternalLogin
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult ExternalLogin(string provider, string returnUrl)
    {
        // https://stackoverflow.com/questions/20737578/asp-net-sessionid-owin-cookies-do-not-send-to-browser
        Session["Workaround"] = 0;
        // Request a redirect to the external login provider
        return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
    }
Run Code Online (Sandbox Code Playgroud)

  • 问题似乎在于owin - 所有你应该知道的是要记住在服务之前首先明确地设置cookie(owin).解决方案Marc发布的是通过将会话(曾经命名的)设置为某个值来嘲笑它. (2认同)

Ikr*_*hah 6

我遇到了Visual Studio 2017.net MVC 5.2.4的类似问题,将 Nuget Microsoft.Owin.Security.Google更新到最新版本,目前4.0.1对我有用

  • 就我而言,从 4.0.0 更新到 4.1.0 后它就可以工作了。 (3认同)