身份验证Cookie在第三方Iframe中不起作用

问题描述

我有一个.net MVC 5应用程序。 AuthCookie可以在iframe上正常工作,我可以看到SameSite策略设置为None。但是,在iframe中,[Authorize]属性方法固定在永久重定向上。看来,在iframe中,请求无法读取由以下代码创建的cookie。

followed the documentation并实施了SameSiteCookieManager.cs

Web配置

<httpCookies sameSite="None" requireSSL="true" />
<sessionState cookieSameSite="None">
</sessionState>
<authentication mode="None" />
<compilation targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2" />

启动

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,LoginPath = new PathString("/Account/Login"),Provider = new CookieAuthenticationProvider
            { 
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager,ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),regenerateIdentity: (manager,user) => user.GenerateUserIdentityAsync(manager))
            },CookieManager = new SameSiteCookieManager(new SystemWebCookieManager()),CookieSameSite = SameSiteMode.None,CookieHttpOnly = true,CookieSecure = CookieSecureOption.Always,ExpireTimeSpan = new TimeSpan(0,10,0)
        });

帐户/登录

 var user = UserManager.Find(userName,"*****");
 AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
 var identity = UserManager.CreateIdentity(user,DefaultAuthenticationTypes.ApplicationCookie);
        AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent },identity);
 return RedirectToAction("Index","Home");

解决方法

问题出在浏览器上。张贴在这里以防万一,可以节省一些时间。 原来问题出在我的Chrome隐身浏览器上。我选中了“阻止第三方Cookie”选项。enter image description here

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...