【问题标题】:session always null google chrome netcore mvc iframe会话始终为空 google chrome net core mvc iframe
【发布时间】:2021-02-21 02:27:03
【问题描述】:

我有一个网络核心 MVC 项目,它为外部网站提供 iframe。但是在其他网站上使用 iframe 时会话会丢失。

   services.AddSession(opt=> {
              opt.Cookie.SameSite = SameSiteMode.None;
              opt.Cookie.HttpOnly = true;
              opt.Cookie.IsEssential = true;
          });

   services.Configure<CookiePolicyOptions>(options =>
          {
              options.MinimumSameSitePolicy = SameSiteMode.None;
              options.CheckConsentNeeded = context => true;
          });

          services.ConfigureApplicationCookie(opt =>
          {
              opt.Cookie.IsEssential = true;
              opt.Cookie.HttpOnly = true;
              opt.Cookie.Name = "MyCookie";                
              opt.Cookie.SameSite = SameSiteMode.None;
              opt.LoginPath = new PathString("/Home/Index");
          });

          
          services.AddAntiforgery(o => {
              o.SuppressXFrameOptionsHeader = true;
              o.Cookie.SameSite = SameSiteMode.None;
          }) ;

当我在 google chrome 上使用上面的代码时,会话不起作用。我还尝试了其他相同站点的 mods(Lax、Strict、Unspecified)。 你能看到代码中的错误吗?或者你知道怎么做吗?

【问题讨论】:

标签: google-chrome asp.net-core session iframe asp.net-core-mvc


【解决方案1】:

使用options.CheckConsentNeeded = context =&gt; false; 代替options.CheckConsentNeeded = context =&gt; true;

试试吧,我想它会奏效的。

services.Configure<CookiePolicyOptions>(options =>
        {                
            options.CheckConsentNeeded = context => false;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

【讨论】:

  • 当 iframe 内容更改时,我会丢失会话和 cookie。第一次运行 iframe 时一切正常(第一个内容,第一页)
  • 我创建新项目并使用相同的 iframe src 和工作。虽然我多次清除浏览器历史记录和缓存,但不知何故无法在google chrome上运行。我猜出现这种情况是因为调用iframe的项目有ssl
  • 你打电话给app.useSession()
猜你喜欢
  • 2021-11-28
  • 2015-10-07
  • 2013-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多