如果我们将 HttpCookie.HttpOnly 设置为 true,则客户端会发生变化

Changes in Client side if we are Setting HttpCookie.HttpOnly as true

在 .net Core 中,我们使用 IAntiforgery 配置防伪造功能以及 [ValidateAntiForgeryToken] 或 AutoValidateAntiforgeryToken 来防止跨站请求伪造 (XSRF/CSRF) 攻击。

在我们使用的中间件中配置防伪功能

var antiforgery = app.Services.GetRequiredService<IAntiforgery>();

app.Use((context, next) =>
{
    var requestPath = context.Request.Path.Value;

    if (string.Equals(requestPath, "/", StringComparison.OrdinalIgnoreCase)
        || string.Equals(requestPath, "/index.html", StringComparison.OrdinalIgnoreCase))
    {
        var tokenSet = antiforgery.GetAndStoreTokens(context);
        context.Response.Cookies.Append("XSRF-TOKEN", tokenSet.RequestToken!,
            new CookieOptions { HttpOnly = false });
    }

    return next(context);
});

Microsoft Doc Link

现在我的问题是如果我们设置 new CookieOptions { HttpOnly = True }); 那么我们需要在服务器端和客户端做哪些更改

客户端有变化?实际上,绝对 none.

使用 HTTPOnly cookie 比手动提取和存储客户端 cookie/token 应该更容易。 HttpOnly cookie 只是阻止 cookie 被客户端拦截 JavaScript。只要您实际上没有尝试从请求中获取该 cookie(您为什么要这样做,它存储在 cookie 中!),它就会自动与您的所有请求一起发送。

服务器端应该和往常一样工作。 HttpOnly 是客户端更改