我使用的是内置的ASP.NET核心CookieConsent功能,没有任何修改:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}使用客户端JavaScript:
<script>
(function () {
document.querySelector("#cookieConsent button[data-cookie-string]").addEventListener("click", function (el) {
document.cookie = el.target.dataset.cookieString;
document.querySelector("#cookieConsent").classList.add("hidden");
}, false);
})();
</script>在部署到生产环境中时,行为是随机的:有时cookie获得正确的名称,有时cookie变得不定义。经过一些调查,data-cookie-string=".AspNet.Consent=yes; expires=Sat, 10 Oct 2020 09:56:49 GMT; path=/; secure; samesite=lax">总是相同的(除了更改日期),问题似乎是与document.cookie命令有关。
浏览器(Google Chrome最新版本)存储名称为空、值未定义的cookie。
有人经历过这样的行为吗?谢谢
发布于 2019-10-11 18:36:53
expires=日期格式似乎有问题。这个测试解决了这个问题:
<script>
(function () {
document.querySelector("#cookieConsent button[data-cookie-string]").addEventListener("click", function (el) {
var now = new Date();
var expires = (new Date(now.getFullYear() + 1, 12, 31).toUTCString());
document.cookie = '.AspNet.Consent=yes;expires=' + expires + ';path=/;';
document.querySelector("#cookieConsent").classList.add("hidden");
}, false);
})();
</script>https://stackoverflow.com/questions/58338842
复制相似问题