简体   繁体   English

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

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

In .net Core with we use Configure antiforgery features with IAntiforgery along with [ValidateAntiForgeryToken] or AutoValidateAntiforgeryToken to Prevent Cross-Site Request Forgery (XSRF/CSRF) attacks.在 .net 核心中,我们使用 IAntiforgery 配置防伪功能以及 [ValidateAntiForgeryToken] 或 AutoValidateAntiforgeryToken 来防止跨站点请求伪造 (XSRF/CSRF) 攻击。

To configure antiforgery feature in middleware we use要在中间件中配置防伪功能,我们使用

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 微软文档链接

Now my Question is If We set new CookieOptions { HttpOnly = True });现在我的问题是如果我们设置new CookieOptions { HttpOnly = True }); then what changes do we need to do at server side as well as client side那么我们需要在服务器端和客户端做哪些改变

Changes on client-side?客户端的变化? Realistically, absolutely none.实际上,绝对没有。

It should be easier to use an HTTPOnly cookie rather than extracting and storing your client-side cookie/token manually.使用 HTTPOnly cookie 应该比手动提取和存储客户端 cookie/令牌更容易。 The HttpOnly cookie just stops the cookie from being interceptable via client-side JavaScript. HttpOnly cookie 只是阻止 cookie 被客户端 JavaScript 拦截。 As long as you aren't actually trying to grab that cookie from the request (and why would you, it's stored in cookies,).只要您实际上并没有尝试从请求中获取该 cookie(为什么会,它存储在 cookies 中)。 then it will automatically be sent along with all of your requests.然后它将与您的所有请求一起自动发送。

Server-side should work the same as always.服务器端应该像往常一样工作。 HttpOnly is a client-side change HttpOnly 是客户端更改

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

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