【发布时间】:2020-02-12 21:12:29
【问题描述】:
至少几年来,我一直在我的 MVC 解决方案中使用与此类似的代码...
[Authorize]
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
..........
然后在我的验证码中
myAuthenticationProperties = new Microsoft.Owin.Security.AuthenticationProperties();
myAuthenticationProperties.AllowRefresh = true;
myAuthenticationProperties.ExpiresUtc = DateTime.UtcNow.AddMinutes(60);
myAuthenticationManager.SignIn(myAuthenticationProperties, myClaimsIdentity);
return RedirectToAction("Index", "Home");
在我的启动中..
public void Configuration(IAppBuilder app)
{
CookieAuthenticationOptions myAuthOptions = new CookieAuthenticationOptions();
myAuthOptions.AuthenticationType = "ApplicationCookie";
myAuthOptions.CookieHttpOnly = true;
myAuthOptions.SlidingExpiration = true;
myAuthOptions.LoginPath = new PathString("/Authentication/LogIn");
//This is what was added for the Owin cookie "fix"
myAuthOptions.CookieSameSite = SameSiteMode.Strict;
myAuthOptions.CookieSecure = CookieSecureOption.Always;
app.UseCookieAuthentication(myAuthOptions);
}
生活一直很美好……直到现在。我一直在到处追赶我的尾巴,试图弄清楚为什么当我尝试登录时,有时它可以工作,而有时它只是挂起。使用一些调试消息,我发现我的身份验证过程完成了,但是当 RedirectToAction 发生时,什么都没有发生......只是挂起。
然后我有一个突破,我尝试使用 IE 和 Edge,似乎每次都能正常工作。只有 Chrome 会挂起,而且至少有 75% 的时间会挂起。
** 更新 **
我已经使用了 Fiddler 和 Chrome 的调试(控制台和网络选项卡),当 RedirectToAction 发生时,就网站而言,它已经完成。然而,没有任何东西,我的意思是没有,回到我的客户端的网络上(根据 Fiddler 和 Chrome 的网络)。
然而,如果我手动更改 url 以返回主页,Chrome 很高兴,我现在已通过身份验证,我的 [Authorize] 现在允许加载控制器。
我已经研究了新的 Chrome cookie 问题,尽管“修复”似乎一清二楚,但我还是找到了使用代码强制 SameSite cookie 报告 LAX 以外的其他内容的人。我实现了这一点,实际上将其设置为“严格”并且仍然...... Chrome 挂起。
** 创可贴**
我不知道这会给我带来多少时间,但我通过使用 Javascript 计时器解决了这个问题,当用户单击提交按钮时,计时器启动,等待 6 秒,然后重定向回来到主页/索引。
如果问题不存在(IE、Edge),客户端会在计时器有机会占用之前自动重定向。如果他们正在使用 Chrome 并且它决定挂起,6 秒后它会表现得好像他们的浏览器很慢,并且会将他们带到正确的位置。
** 已修复(可能)**
因此,即使没有看到返回客户端的网络流量,我最终(除了上面的创可贴之外)实施了一些额外的更改,因此现在 Owin 和 Asp.net cookie 都报告了安全和相同站点 = 严格。这似乎对我的问题产生了影响,如果它仍然想挂起,我的定时重定向会解决问题。
对于那些可能也会遇到这种奇怪情况的人来说,Cookie 修复的要点是......
- 更新您的 Owin 软件包以确保您使用的是 4.1 版
- 将 Startup.cs 中的 CookieAuthenticationOptions 调整为我在上面添加的项目,以使 Owin cookie 兼容。
-
在您的 Web.config 中更新以下内容以使您的 Asp.net cookie 兼容
<system.web> <sessionState cookieSameSite="Strict" /> <httpCookies requireSSL="true" /> </system.web>
执行这 3 件事(以及在 SSL 下运行您的项目)将导致 Chrome 将 cookie 报告为安全和严格。
【问题讨论】:
-
嗨 - 是否有解决方案,无需将本地项目作为 https 运行?
标签: asp.net-mvc google-chrome owin