谷歌 aspnet mvc5 上的 AuthenticationManager.GetExternalLoginInfoAsync() 返回 null [重复]

2023-11-22

我使用默认的 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);
    }
    
  • 在外部登录回调上

    //
    // 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");
        }
    ...
    

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

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

未复制
我已经发布了答案,但答案不在其中OWIN 的 GetExternalLoginInfoAsync 始终返回 null帖子中,我提到了我找到解决方案的线程:ASP.NET_SessionId + OWIN Cookie 不发送到浏览器


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

Session["Workaround"] = 0;

在上面的线程(以及那里提供的链接)中,找到了混合 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 }));
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

谷歌 aspnet mvc5 上的 AuthenticationManager.GetExternalLoginInfoAsync() 返回 null [重复] 的相关文章

随机推荐

  • CodeIgniter 框架上有类似 MasterPages 的东西吗?

    我是 Code Igniter 的新手 我想知道是否有任何东西可以像 NET 上的 MasterPages 一样工作 我还想知道我应该在哪里保存我的公共文件 例如脚本 样式和图像 问候 并预先感谢您 主视图未内置到框架中 要获得类似的效果
  • SSL证书是否绑定到服务器IP地址?

    我们在两个不同的物理办公地点有两个不同的 LDAP 提供商 当我将笔记本电脑连接到一个位置并 从端口检索 在 Websphere 6 1 中 以导入 ldap 提供者的 SSL 证书时 我可以毫无问题地对相应的 ldap 进行身份验证 如果
  • 此 COUNT MySQL 语句中出现未知列错误?

    错误是 where 子句中的未知列 num SELECT COUNT AS num books bookid FROM bookgenre has books WHERE num gt 10 GROUP BY books bookid 我究
  • 如何从 Google Places API 获取国家/地区代码

    我正在尝试使用 HTML 5 GeoLocation 来获取经度和纬度 然后使用 Google Maps API获取国家 地区代码该经度 纬度 有没有更简单的方法从 google place api 获取国家 地区代码 我从这个链接找到了解
  • Ruby 中的正则表达式负向后查找似乎不起作用

    制作一个参数解析器 我想将一个字符串分割成一个数组 其中分隔符是 除非前面有 这意味着字符串 foo ba r arg 应该导致 foo ba r arg 我正在尝试使用这个正则表达式
  • 如何使用用户凭据在 Powershell 中运行 Start-Process?

    我有一个 Windows 服务 Jenkins 它运行一个需要以特定用户身份运行命令的脚本 我尝试这样做 但它不起作用 secpasswd ConvertTo SecureString myPassword AsPlainText Forc
  • Admob 广告未展示 - Android

    我的广告根本不显示 我认为我已正确遵循文档 但它们仍然不会显示 该程序基本上是一个网络视图 我希望广告显示在底部 这是我的布局文件
  • 即使在“keep class”标志之后,ProGuard 也会混淆类。影响 Android WebView 行为

    我正在使用 ProGuard 来混淆我的 Android 应用程序 我也在用WebView显示一个网页 HTML 演练页面 其中包含一个可关闭该按钮的按钮WebView Javascript中有一个函数可以回调closeWalkthroug
  • 在appdata文件夹中创建sql server压缩文件

    我正在开发一个简单的软件 它首先使用实体 框架代码和sql server Compact 4 目前此设置有效 如果 sql server 压缩文件尚不存在 实体框架将创建该文件 数据库的路径是从存储在 app config 文件内的连接字符
  • Google OAuth 登录卡在加载同意屏幕上

    我的应用程序使用 Google Drive API 来备份用户文件 我想从头开始测试我的应用程序登录 因此我从我的 Google 帐户设置中手动撤销了该应用程序 但当我再次登录时 我在选择我的 Google 帐户后卡住了加载同意屏幕 见下文
  • 算法:将列表从一种顺序重新排列为另一种顺序的最佳方法?

    EDIT 我不确定我原来的问题是否足够清楚 我需要一种算法来计算将数组从一个顺序重新排列为另一个顺序的最小移动序列 众所周知 两个数组将包含相同的元素 没有重复 并且具有相同的长度 例如 reorder d a c b e a b c d
  • AWS Elasticbeanstalk 使用 .platform 覆盖 Nginx 配置不起作用

    我正在将 Laravel 应用程序部署到 AWS ElasticBeanstalk 我已经部署了 现在 我尝试使用 platform 文件夹覆盖 etc nginx conf d elasticbeanstalk php conf 文件 我
  • C 中克罗内克乘积的高效计算

    我对 C 相当陌生 对于我的大部分研究来说 不需要比 python 更快的东西 然而 事实证明我最近所做的工作需要计算相当大的向量 矩阵 因此 C MPI 解决方案可能是合适的 从数学上来说 任务非常简单 我有很多维数约为 40k 的向量并
  • python的帧缓冲模块

    我正在寻找一个可以显示的python模块jpg or png文件至 dev fb0直接地 我希望模块可以像这样调用并在屏幕上显示图片 show photo path to jpg x y dev dev fb0 我在google上搜索了这种
  • 使用 html 按钮更改网站语言

    在 PHP 中 我想在单击按钮时更改网站的语言 英语 德语等 这是解决该问题的正确方法吗 a href index php img src images language languageNO png a a href index php
  • 对方法返回值使用 C# 丢弃运算符是否有意义?

    Visual Studio 2019 的代码分析和代码建议开始突出显示我调用返回值但根本不使用该值的方法的每一行代码 并告诉我使用丢弃运算符 我不完全理解为什么这很重要 甚至对于 Fluent API 风格的代码来说它似乎是错误的 以下两行
  • Autofac - 生命周期和模块

    问题 摘要 给定一个注册依赖项 X 的模块 依赖项 X 在 MVC3 应用程序中具有不同的生命周期 每个 HttpRequest 的生命周期 然后在控制台应用程序中 每个具有名称的生命周期范围的依赖项 在哪里或如何指定依赖项 X 的生命周期
  • 使用 XSD 正确验证 XML 文档

    作为一名具有丰富 XML 使用和生成经验的开发人员 我以前从未真正与模式进行过交互 这对我来说是第一次真正发生 我遇到过一个 功能 我认为它更像是一个有详细记录的错误 使用 XDocument Validate 时 似乎在某些情况下 如果文
  • OnApplicationFocus() 和 OnApplicationPause() 有什么区别?

    说到移动设备 这两种方法有什么区别 如果我按主页键 两者都会被调用 有没有一种情况 一个被调用 另一个不被调用 由于这个 UnityAnswer 是第一个 如果不是第一个 在搜索 OnApplicationFocus Pause 和 iOS
  • 谷歌 aspnet mvc5 上的 AuthenticationManager.GetExternalLoginInfoAsync() 返回 null [重复]

    这个问题在这里已经有答案了 我使用默认的 Visual Studio 2015 模板和 Google 身份验证开发了 ASPNET MVC 5 应用程序 在开发环境中一切正常 但在外部身份验证后的实际调用中AuthenticationMan