AspNet Core Identity, how set options.Cookie.SameSite?

被刻印的时光 ゝ 提交于 2019-12-01 14:39:28

问题


In the latest templates and libraries used httpsonly flag. How can I turn it off?

This same question is outdated and it did not have full configuration sample:

AspNet Core Identity - cookie not getting set in production


回答1:


In order to configure the application cookie when using Identity, you can use the ConfigureApplicationCookie method inside your Startup’s ConfigureServices:

// add identity
services.AddIdentity<ApplicationUser, IdentityRole>();

// configure the application cookie
services.ConfigureApplicationCookie(options =>
{
    options.Cookie.SameSite = SameSiteMode.None;
});

Since Identity essentially adds cookie authentication under the hood, this is the configure action is the same thing you would normally pass to AddCookie() when configuring cookie authentication. It’s just that since AddIdentity() takes care of setting up authentication for you, the ConfigureApplicationCookie offers a way to adjust the cookie authentication options afterwards.




回答2:


The answer by @poke did not help me set the value to SameSiteMode.None, atleast not in ASP.NET core 2.1.

Any value you set in configure application cookie is overridden by the MinimumSameSitePolicy setting of the cookie policy middleware.

This prevent the override, set MinimumSameSitePolicy for the UseCookiePolicy extension as SameSiteMode.None.

app.UseCookiePolicy(new CookiePolicyOptions
{
   MinimumSameSitePolicy = SameSiteMode.None
});

Then set the actual same site value in the AddCookie extension in the ConfigureServices method

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
    options => options.Cookie.SameSite = SameSiteMode.None;
});


来源:https://stackoverflow.com/questions/48051969/aspnet-core-identity-how-set-options-cookie-samesite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!