【问题标题】:Asp.NET Identity stopped working on ChromeAsp.NET Identity 在 Chrome 上停止工作
【发布时间】:2016-05-15 22:34:33
【问题描述】:

由于某种未知原因,我无法再在 Chrome 上登录自己的应用程序。

这就是我登录的方式:

    [AllowAnonymous]
    [HttpPost]
    public async Task<ActionResult> Login(LoginModel model)
    {
        if (User.Identity.IsAuthenticated)
            return RedirectToAction("Index", "Home");

        if (!ModelState.IsValid)
            return View();

        var status = await signInManager.PasswordSignInAsync(model.Login, model.Password, true, false);
        switch (status)
        {
            case Microsoft.AspNet.Identity.Owin.SignInStatus.Success:
                {
                    int userId = User.Identity.GetUserId<int>();
                    if (userManager.IsFirstLogin(userId))
                    {
                        return RedirectToAction("Declaration", "Home", new { returnUrl = model.ReturnUrl });
                    }

                    return RedirectToLocal(model.ReturnUrl);
                }
            default:
                return View();
        }
    }

我调试了代码并且 PasswordSignInAsync 成功,但是 Chrome 显示没有为页面设置 cookie。实际上重定向到动作,用[Authorize]属性标记再次重定向到登录页面。

我在 IE 上测试了网站,一切正常,我可以登录和退出,并且 cookie 设置正确。

如果我能提供更详细的信息,请告诉我。


编辑:更多细节

我一直在测试一个对日期时间敏感的应用程序(在特定日期之后停止显示)。我将系统时间更改为 2016 年 8 月 1 日,登录然后切换回现在。那时一切都停止了。

奇怪的是,当我切换回那个未来的时间时,我可以再次登录并且会话 cookie 设置正确。但是当我回到“现在”时,我无法再次登录。

【问题讨论】:

  • 在使用 Chrome 和 IE 时,Fiddler 跟踪显示有什么不同吗?
  • 嗨@Spook,你解决了吗?我也遇到了类似的问题
  • @RicoW 我最终做到了,但那是很久以前的事了 :) 据我记得,我不得不强制擦除该站点的浏览器缓存。
  • 你好 - 有谁知道解决这个问题的正确方法吗?

标签: asp.net google-chrome cookies asp.net-identity


【解决方案1】:

仍然需要调查发生了什么,但对我来说,“修复”是更改 Auth cookie 的名称。

在 Startup.Auth.cs 中:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    CookieName = $"SomeCustomNameForAuthCookie",
    Provider = ...
}); 

我怀疑这样一个事实,即我的计算机上有很多网站可供开发,并且都具有相同的默认 cookie 名称。

【讨论】:

  • 这对我不起作用。使用 Owin 4.1 和 chrome 84
  • 很难说,您在本地 IIS 中注册了其他项目吗?当您在某处发布应用程序时也会发生这种情况吗?你不会不小心超过 cookie 大小(据我记得是 2kB)吗?
【解决方案2】:

我在我的应用程序中遇到了同样的问题,但在我的情况下,我的应用程序是在 iframe 中打开的,并且在登录时 cookie 未设置到浏览器中,因为发生了错误并且大于 80 的 chrome 版本更改了 cookie 策略。我们需要在 web.config 文件中设置这段代码

<httpCookies sameSite="None" requireSSL="true" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 2012-08-18
    • 2011-09-21
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    相关资源
    最近更新 更多