noobProgrammer
noobProgrammer

Reputation: 2984

asp.net core session not working , set cookie in response header but not set in browser

I am using sessions to manage application state in ASP.NET CORE and it's configured as below.

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

It's working on localhost but on remote IIS 8 it's not creating cookies so not able to get the values. I also have enabled CORS and don't know what exactly caused this problem. In log it's not showing error either. In response header set cookie is present but not set in browser

Upvotes: 3

Views: 2984

Answers (2)

H. Eberhardt
H. Eberhardt

Reputation: 79

I had this problem some time ago. It may be related with the new Cookie Policy.

Try to set options.CheckConsentNeeded = context => false;. So inside "ConfigureServices" in Startup.cs it need to be like this:

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();
        }

Regards,

H. Eberhardt

Upvotes: 3

Avadhesh Jadon
Avadhesh Jadon

Reputation: 23

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

May be it's working, Try it now

Upvotes: 2

Related Questions