【问题标题】:AntiForgery implementation in Asp.net FormsAsp.net Forms 中的 AntiForgery 实现
【发布时间】:2010-07-20 15:08:31
【问题描述】:

我正在开发一个 httphandler 来处理 Web 表单中的一些请求(不在 MVC 中)。
如何实现反跨站点脚本(如 MVC 中的防伪)?
我想了解 MVC 中的防伪机制。

【问题讨论】:

  • 所有有用的类/方法都是私有的、受保护的或内部的,因此在 ASP.NET MVC 应用程序之外没有用处。

标签: asp.net forms httphandler antiforgerytoken


【解决方案1】:

如果您可以访问页面,则可以使用页面的ViewStateUserKey 属性。以下是如何在页面内执行此操作的示例,但您会明白的:

protected void Page_Init(object sender, EventArgs e)
{
    // Validate whether ViewState contains the MAC fingerprint
    // Without a fingerprint, it's impossible to prevent CSRF.
    if (!this.Page.EnableViewStateMac)
    {
        throw new InvalidOperationException(
            "The page does NOT have the MAC enabled and the view" +
            "state is therefore vulnerable to tampering.");
    }

    this.ViewStateUserKey = this.Session.SessionID;
}

虽然 ViewStateUserKey 相当安全,但也有一些缺点。你可以阅读更多关于here的信息。

【讨论】:

  • 谢谢 我还实现了以下部分: 1- 检查 http 方法是否为 POST。 2-检查 UrlRefferrer 是否为相同的域名。 3-我将添加您的解决方案。如果黑客在浏览 Web 应用程序时更改了他/她的 DNS,这会不会很危险?
猜你喜欢
  • 1970-01-01
  • 2014-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
  • 2017-05-20
  • 2017-12-04
  • 1970-01-01
相关资源
最近更新 更多