asp.net核心会话无法正常工作,在响应头中设置cookie但未在浏览器中设置

时间:2016-06-28 01:53:41

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

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

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

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

2 个答案:

答案 0 :(得分:3)

我前一段时间有这个问题。可能与新的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。埃伯哈特

答案 1 :(得分:1)

services.AddSession(options => 
{ 
    options.IdleTimeout = TimeSpan.FromMinutes(20); 
    options.CookieHttpOnly = true; 
});

可能正在工作,立即尝试

相关问题