首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义ASP.NET核心CookieConsent集

未定义ASP.NET核心CookieConsent集
EN

Stack Overflow用户
提问于 2019-10-11 18:00:42
回答 1查看 413关注 0票数 2

我使用的是内置的ASP.NET核心CookieConsent功能,没有任何修改:

代码语言:javascript
复制
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:

代码语言: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。

有人经历过这样的行为吗?谢谢

EN

回答 1

Stack Overflow用户

发布于 2019-10-11 18:36:53

expires=日期格式似乎有问题。这个测试解决了这个问题:

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58338842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档