【问题标题】:asp.net core session not working , set cookie in response header but not set in browserasp.net 核心会话不起作用,在响应标头中设置 cookie 但未在浏览器中设置
【发布时间】:2016-10-30 04:06:00
【问题描述】:

我正在使用会话来管理 ASP.NET CORE 中的应用程序状态,它的配置如下。

 services.AddSession(options =>
            {
                options.CookieName = ".my.Session";
                options.IdleTimeout = TimeSpan.FromSeconds(20);
            });

它在本地主机上工作,但在远程 IIS 8 上它没有创建 cookie,因此无法获取值。我也启用了 CORS,但不知道究竟是什么导致了这个问题。在日志中也没有显示错误。 响应头设置 cookie 存在但未在浏览器中设置

【问题讨论】:

  • 您找到解决方案了吗?我这里也有同样的问题。

标签: cookies browser asp.net-core setcookie cookie-httponly


【解决方案1】:

我前段时间遇到过这个问题。可能与新的 Cookie 政策有关。

尝试设置options.CheckConsentNeeded = context => false;。因此,在 Startup.cs 的“ConfigureServices”中,它需要是这样的:

public void ConfigureServices(IServiceCollection services)
        {
            var connection = Configuration["ConnectionStrings:DefaultConnection"];
            services.AddDbContext<ProjectDbContext>(options => options.UseMySql(connection, b => b.MigrationsAssembly("PrimaryProject")));

            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.

                //Here comes the change:
                options.CheckConsentNeeded = context => false;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });



            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseMySql(connection));
            services.AddDefaultIdentity<IdentityUser>()
                .AddEntityFrameworkStores<ApplicationDbContext>();



            services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
                .AddSessionStateTempDataProvider();

            services.AddSession();
        }

问候,

H.埃伯哈特

【讨论】:

    【解决方案2】:
    services.AddSession(options => 
    { 
        options.IdleTimeout = TimeSpan.FromMinutes(20); 
        options.CookieHttpOnly = true; 
    });
    

    也许它起作用了,现在就试试吧

    【讨论】:

      猜你喜欢
      • 2019-04-26
      • 2014-08-24
      • 2018-08-22
      • 2020-10-07
      • 2019-06-08
      • 2012-05-02
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      相关资源
      最近更新 更多