【问题标题】:How to enable HttpOnly cookies on ASP.NET Core 2.1 Web site如何在 ASP.NET Core 2.1 网站上启用 HttpOnly cookie
【发布时间】:2019-01-08 09:05:44
【问题描述】:

我在 Visual Studio 中使用 File->New Project 创建了一个标准的 ASP.NET Core MVC 网站和 Core 2.1。

在 Startup.cs 中是样板代码

 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_1);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

当我浏览该站点时,当我接受 cookie 策略时,会有一个 .AspNet.Consent cookie。它默认标记为安全,但不是 httponly。

如何在所有 cookie 上启用 HttpOnly?

谢谢。

【问题讨论】:

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


    【解决方案1】:

    你试过了吗?

    services.ConfigureApplicationCookie(options =>
    {
        // Cookie settings
        options.Cookie.HttpOnly = true;
        options.ExpireTimeSpan = TimeSpan.FromMinutes(10);
    
    });
    

    【讨论】:

    • 这不是所提问题的最佳答案,但它是解决“同意”cookie 的具体问题的最佳方法,就像最近在 Chrome 80 中发生的那样。
    • 是否可以创建aspnet核心非持久cookie?
    【解决方案2】:

    同意 cookie 不是 HttpOnly,因为它是通过 JavaScript 在客户端设置的。你可以在_CookieConsentPartial.cshtml找到代码:

    <script>
        (function () {
            var button = document.querySelector("#cookieConsent button[data-cookie-string]");
            button.addEventListener("click", function (event) {
                document.cookie = button.dataset.cookieString;
            }, false);
        })();
    </script>
    

    如果您需要 HttpOnly cookie,您应该自己在中间件或控制器中实现同意逻辑,并使用带有 POST 请求的常规表单。

    【讨论】:

    • 我认为这是最好的答案。似乎唯一的方法是连接到 Microsoft 的 cookie 中间件并在他们的 cookie 上设置 HttpOnly。
    【解决方案3】:

    手动设置 cookie 时(例如针对 HTTPContext),有一个简单的 CookieOptions 对象可用于将 HttpOnly 设置为 true。它最终看起来有点像这样:

    HttpContext.Response.Cookies.Append(
    "CookieKey",
    "CookieValue",
    new CookieOptions
    {
        HttpOnly = true
    });
    

    Microsoft 有一个使用 cookie 进行身份验证的中间件。如果要在应用中使用它,请将其添加到 startup.cs 的 Configure 方法中。

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseMvc();
        app.UseCookieAuthentication();
    }
    

    如果您以这种方式使用CookieAuthentication,则默认使用HttpOnly cookies。更多详情请参考here

    【讨论】:

    • 谢谢,我已阅读该网站,但无法获得任何可行的建议。
    猜你喜欢
    • 1970-01-01
    • 2019-02-10
    • 2020-06-19
    • 1970-01-01
    • 2011-10-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多