【问题标题】:How to prevent CSRF attack in asp.net webform?如何防止asp.net webform中的CSRF攻击?
【发布时间】:2011-03-12 03:24:13
【问题描述】:

如何防止 ASP.NET WebForms 中的 CSRF (Cross-site Request Forgery) 攻击?

在 ASP.NET MVC 中有没有类似[ValidateAntiForgeryToken] 的东西?

【问题讨论】:

标签: webforms


【解决方案1】:

当您谈论保护 ViewState 时,您可以使用“ViewStateUserKey”。

基本上,您需要为每个用户使用一个特定的密钥,该密钥源自 ASP.NET 会话。这是一个例子:

/// <summary>
///     Raises the <see cref="E:System.Web.UI.Control.Init" /> event to initialize the page.
/// </summary>
/// <param name="e">
///     An <see cref="T:System.EventArgs" /> that contains the event data.
/// </param>
protected override void OnInit(EventArgs e) {
    base.OnInit(e);

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

    ViewStateUserKey = Session.SessionID;
}

您可以了解更多信息,例如from the Microsoft Docs.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 2018-01-08
    • 2015-09-28
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    相关资源
    最近更新 更多