Chrome不会在身份验证处理后重定向回URL

问题描述 投票:0回答:1

至少在几年前,我一直在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的Debugging(“控制台”和“网络”选项卡,并且当RedirectToAction发生时,就网站而言,它已经完成。但是,什么也没有,也没有任何意思,会通过网络返回到客户端(根据Fiddler和Chrome的网络)。

但是,如果我手动更改网址返回首页,Chrome很高兴,我现在已经通过身份验证,并且我的[Authorize]现在允许加载控制器。

我已经研究了新的Chrome Cookie内容,尽管“修复”似乎很泥泞,但我能够找到有人使用代码来强制SameSite Cookie报告除LAX以外的内容。我实现了它,实际上将其设置为“严格”,但仍然... Chrome挂起。

**创可贴**

我不知道这要花多少时间,但是我已经通过使用Javascript计时器来解决这个问题,该计时器在用户单击“提交”按钮时启动,计时器等待6秒,然后重定向回主页/索引。

如果问题不存在(IE,Edge),则客户端会在计时器有机会抓住机会之前自动重定向。如果他们使用的是Chrome并决定挂起,则6秒钟后,它的行为就像他们的浏览器很慢一样,也会将它们带到正确的位置。

**固定(也许)**

因此,即使看不到网络流量返回客户端,我还是结束了(除了上面的创可贴之外),实施了一些其他更改,因此现在Owin和Asp.net cookie都报告了Secure和sameSite =严格。这似乎与我的问题有所不同,并且在仍然要挂起的情况下,我的定时重定向可以解决问题。

对于那些可能也会遇到这种奇怪情况的人,Cookie修正的要旨是这个...

  1. 更新您的Owin软件包以确保您使用的是4.1版
  2. 将您在Startup.cs中的CookieAuthenticationOptions调整为我在上面添加的项目,以使Owin Cookie兼容。
  3. 更新Web.config中的以下内容以使您的Asp.net Cookie兼容

    <system.web>
       <sessionState cookieSameSite="Strict" />
       <httpCookies requireSSL="true" />
    </system.web>
    

执行这三件事(以及在SSL下运行项目)将导致Chrome将Cookie分别报告为安全和严格。

asp.net-mvc google-chrome owin
1个回答
0
投票

Google对how Chrome handles cookies进行了更改,但没有SameSite属性。以前,Chrome将未在Cookie上设置SameSite属性与将SameSite=None设置为相同,这意味着浏览器将接受所有cookie。现在,他们将其视为具有SameSite=Lax,它将仅接受来自同一域的cookie。为了获得与旧方法相同的效果,必须将属性设置为SameSite=None; Secure

© www.soinside.com 2019 - 2024. All rights reserved.