QQ浏览器、搜狗浏览器等兼容模式下,Asp.NetCore下,Cookie、Session失效问题
这些狗日的浏览器在兼容模式下,保存Cookie会失败,是因为SameSiteMode默认为Lax。
有时页面需要用到验证码时,可能需要TempData保存一下,但是这种情况是无效的,因此需要用Session来存取,因为Session可以配置SameSiteMode
废话不多说,要修改的位置有3个,均是在Startup里面,设置完成即可
services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => false; options.MinimumSameSitePolicy = SameSiteMode.None; options.ConsentCookie.SameSite = SameSiteMode.None; });
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => { options.LoginPath = new PathString("/account/login"); options.AccessDeniedPath = new PathString("/denied"); options.Cookie.Name = ".demo"; options.Cookie.HttpOnly = true; options.Cookie.SameSite = SameSiteMode.None; });
services.AddSession(options => { options.Cookie.SameSite = SameSiteMode.None; });
Empower every person and every organization achieve more in my blog.