gpt4 book ai didi

asp.net-mvc-4 - AntiForgeryToken,特别是 'match up' 是如何用 cookie 的?

转载 作者:行者123 更新时间:2023-12-02 01:33:52 25 4
gpt4 key购买 nike

我们有一个网页,上面有一个表单。有一个 antiforgerytoken 通过 helper 添加到这个 MVC 页面的表单中:

@Html.AntiForgeryToken()

我们今天意识到我们已经缓存了页面,我们认为这将是一个大问题,但是多台机器可能会提交相同的表单,即使它们在页面源中共享相同的验证 token !?

据我所知,这是出乎意料的,我认为这个想法是可以在 cookie 中找到相同的验证 token ?我显然误解了 token 背后的机制(在页面上和用户 cookie 的修改)。

有人可以向我解释此页面如何仍然有效吗?更具体地说,服务器如何验证表单的发布请求。

我认为这就像服务器检查 token 字符串是否与在 cookie 中找到的 token 字符串相同一样简单。

注意:首先,缓存暂时关闭,我们不是 100% 满意缓存带有 token 的页面,因为我们对它有了更多了解。

最佳答案

We realised today that we have the page cached, which we thought would be a massive issue, but multiple machines may submit the same form, even though they share the same verification token in the page's source!?

是的,当涉及到防伪 token 时,缓存是一个问题。您有两个选择:

  1. 不要缓存表单。
  2. 使用 OutputCacheVaryByCustom 属性根据 token 本身变化。

我手边没有代码,所以我将向您展示一个取自 this 的示例文章:

In order to avoid this issue you can use the VaryByCustom property on the OutputCache attribute:

[OutputCache(
Location = OutputCacheLocation.ServerAndClient,
Duration = 600,
VaryByParam = "none",
VaryByCustom = "RequestVerificationTokenCookie")]
public ActionResult Index()
{
return new View();
}

And then program the rule on the global.asax‘s GetVaryByCustomString method:

public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom.Equals("RequestVerificationTokenCookie", StringComparison.OrdinalIgnoreCase))
{
string verificationTokenCookieName =
context.Request.Cookies
.Cast<string>()
.FirstOrDefault(cn => cn.StartsWith("__requestverificationtoken", StringComparison.InvariantCultureIgnoreCase));
if (!string.IsNullOrEmpty(verificationTokenCookieName))
{
return context.Request.Cookies[verificationTokenCookieName].Value;
}
}
return base.GetVaryByCustomString(context, custom);
}

根据评论编辑

它起作用的原因是因为它只是将 cookie 的值与表单上生成的隐藏字段的值进行比较。如果您认为它可能以这种方式工作,则服务器上没有维护的 token 列表可供验证。这意味着,就客户端而言,尽管生成的表单缓存在服务器上,客户端仍会收到"new"表单,因此还会收到一个 cookie,因此比较不会不要失败。

关于asp.net-mvc-4 - AntiForgeryToken,特别是 'match up' 是如何用 cookie 的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32412508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com