c# - 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 是否支持自己的浏览器?
解决方案
推荐阅读
- java - 如何在 Android 中设置 cookie 然后加载 url 以便该 url 域服务器读取 cookie
- asp.net-core-5.0 - 同一服务器上的 .NET Core 2.1 和 .NET Core 5.0 Web 应用
- python - 如何在pygame的标签中放置文本和变量
- c# - C# Razor 重定向到 .NetCore 5 中具有自定义路由的页面
- post - 如果登录 POST 请求中的凭据在没有加密的情况下可见,是否可以?
- jenkins - 如何从jenkins构建参数groovy设置环境变量
- python - 在几次错误猜测后,如何添加提示以显示?
- javascript - 选中单选按钮时不显示 Div
- r - 如何链接浏览器中上传的 Shiny App 和 Rmd?
- typescript - 如何在嵌套对象字段上使用三元运算符来验证我的打字稿类型