首页 > 解决方案 > ASP.NET CreateConsentCookie (_CookieConsentPartial.cshtml) 在 IE 和 Edge 中不起作用

问题描述

我正在使用 ASP.NET 3.1,使用 CookiePolicy 功能

// Startup
services.Configure<CookiePolicyOptions>(options =>
{
    options.CheckConsentNeeded = context => true;
    options.MinimumSameSitePolicy = SameSiteMode.None;
});

// Configure
app.UseCookiePolicy();

这是 Microsoft 提供的默认 _CookieConsentPartial.cshtml

@using Microsoft.AspNetCore.Http.Features

@{
    var consentFeature = Context.Features.Get<ITrackingConsentFeature>();
    var showBanner = !consentFeature?.CanTrack ?? false;
    var cookieString = consentFeature?.CreateConsentCookie();
}

@if (showBanner)
{
    <div id="cookieConsent" class="alert alert-info alert-dismissable show" style="font-weight:bold" role="alert">
        We use cookies to improve your browing experience. Please review our <a asp-page="/privacy">Cookie Policy</a> and click accept to continue.
        <button type="button" class="accept-policy close" data-dismiss="alert" aria-label="Close" data-cookie-string="@cookieString">
            <span aria-hidden="true">Accept</span>
        </button>
    </div>
    <script>
        (function () {
            var button = document.querySelector("#cookieConsent button[data-cookie-string]");
            button.addEventListener("click", function (event) {
                document.cookie = button.dataset.cookieString;
                document.getElementById("cookieConsent").style.visibility = "hidden";
            }, false);
        })();
    </script>
}

它在 Chrome 和 FireFox 中运行良好,但在 IE 和 Edge 中没有设置 cookie。我很惊讶我没有看到任何其他人在网上抱怨这个问题!

我唯一能找到的

在搜索了很多确切答案后,我发现 Internet Explorer(所有版本)不允许您指定 localhost 的域、本地 IP 地址或机器名称。当您这样做时,Internet Explorer 会简单地忽略 cookie。

但是,我没有设置 cookie 域。微软的 CreateConsentCookie 是否支持自己的浏览器?

标签: c#asp.net.netasp.net-mvc

解决方案


推荐阅读