簡體   English   中英

如何在 ASP.NET Core 2.1 Web 站點啟用 HttpOnly cookies

[英]How to enable HttpOnly cookies on ASP.NET Core 2.1 Web site

我使用 File->New Project 在 Visual Studio 中使用 Core 2.1 創建了一個標准的 ASP.NET Core MVC 網站。

在 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?}");
        });
    }

當我瀏覽網站時,當我接受 cookiepolicy 時,會有一個 single.AspNet.Consent cookie。 它默認標記為安全但不是 httponly。

如何在所有 cookies 上啟用 HttpOnly?

謝謝。

你試過這個嗎?

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

});

同意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請求的常規表單。

手動設置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 cookie 有關更多詳細信息,請參閱此處

看起來您可以在 Startup.cs 中使用 IApplicationBuilder.UseCookePolicy 執行此操作:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
       ...
     
        app.UseCookiePolicy(
            new CookiePolicyOptions
            {
                Secure = Microsoft.AspNetCore.Http.CookieSecurePolicy.Always,
                HttpOnly = Microsoft.AspNetCore.CookiePolicy.HttpOnlyPolicy.Always
            });
}

https://learn.microsoft.com/en-us/do.net/api/microsoft.as.netcore.builder.cookiepolicyoptions.httponly?view=as.netcore-2.1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM