繁体   English   中英

如果我们将 HttpCookie.HttpOnly 设置为 true,则客户端的更改

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

在 .net 核心中,我们使用 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);
});

微软文档链接

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

客户端的变化? 实际上,绝对没有。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM