我有一個.NET Webforms站點感謝需要張貼到我的MVC應用程序,當前作爲一個單獨的應用程序位於Webform站點內。在WebForms中生成AntiForgeryToken
Webform應用程序需要將一些敏感值發佈到MVC應用程序。
有沒有辦法在我的WebForms應用程序中生成AntiForgeryToken(),以便它可以通過表單發送。
否則有誰知道任何其他自定義防僞代碼,這將允許我做類似於MVC的AntiForgeryValidation。
我有一個.NET Webforms站點感謝需要張貼到我的MVC應用程序,當前作爲一個單獨的應用程序位於Webform站點內。在WebForms中生成AntiForgeryToken
Webform應用程序需要將一些敏感值發佈到MVC應用程序。
有沒有辦法在我的WebForms應用程序中生成AntiForgeryToken(),以便它可以通過表單發送。
否則有誰知道任何其他自定義防僞代碼,這將允許我做類似於MVC的AntiForgeryValidation。
自己實施並不難。
(如果你看一下MVC的實現,很少有更給它。有幾個輔助方法是你所需要的。)
的WebForms在Page.ViewStateUserKey一個非常類似的模擬。通過setting that to a per-user value(大多數選擇HttpSessionState.SessionId),WebForms將驗證作爲the MAC check的一部分的ViewState 。
overrides OnInit(EventArgs e) {
base.OnInit(e);
ViewStateUserKey = Session.SessionId;
}
有場景中ViewStateUserKey will not help。主要是,他們歸結爲GET請求(或者在Page_Load中沒有檢查IsPostback)做危險的事情,或者禁用ViewStateMAC。
您可以使用反射來獲取用於設置用於MVC驗證的cookie和匹配表單輸入的MVC方法。這樣,您可以通過[AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken]
屬性來進行MVC操作,您可以從WebForms生成的頁面發佈該屬性。
這是一個老問題,但對於Web表單最新的Visual Studio 2012 ASP.NET模板包括烘焙到主網頁防CSRF代碼。如果你沒有模板,這裏是它生成的代碼:
Protected Sub Page_Init(sender As Object, e As System.EventArgs)
' The code below helps to protect against XSRF attacks
Dim requestCookie As HttpCookie = Request.Cookies(AntiXsrfTokenKey)
Dim requestCookieGuidValue As Guid
If ((Not requestCookie Is Nothing) AndAlso Guid.TryParse(requestCookie.Value, requestCookieGuidValue)) Then
' Use the Anti-XSRF token from the cookie
_antiXsrfTokenValue = requestCookie.Value
Page.ViewStateUserKey = _antiXsrfTokenValue
Else
' Generate a new Anti-XSRF token and save to the cookie
_antiXsrfTokenValue = Guid.NewGuid().ToString("N")
Page.ViewStateUserKey = _antiXsrfTokenValue
Dim responseCookie As HttpCookie = New HttpCookie(AntiXsrfTokenKey) With {.HttpOnly = True, .Value = _antiXsrfTokenValue}
If (FormsAuthentication.RequireSSL And Request.IsSecureConnection) Then
responseCookie.Secure = True
End If
Response.Cookies.Set(responseCookie)
End If
AddHandler Page.PreLoad, AddressOf master_Page_PreLoad
End Sub
Private Sub master_Page_PreLoad(sender As Object, e As System.EventArgs)
If (Not IsPostBack) Then
' Set Anti-XSRF token
ViewState(AntiXsrfTokenKey) = Page.ViewStateUserKey
ViewState(AntiXsrfUserNameKey) = If(Context.User.Identity.Name, String.Empty)
Else
' Validate the Anti-XSRF token
If (Not DirectCast(ViewState(AntiXsrfTokenKey), String) = _antiXsrfTokenValue _
Or Not DirectCast(ViewState(AntiXsrfUserNameKey), String) = If(Context.User.Identity.Name, String.Empty)) Then
Throw New InvalidOperationException("Validation of Anti-XSRF token failed.")
End If
End If
End Sub
優秀的文章,但是你錯過了3行,其中聲明瞭AntiXsrfTokenKey和AntiXsrfUserNameKey以及_antiXsrfTokenValue。可能有助於更新一些:-) – EvilDr 2014-07-08 13:44:34
@IanIppolito此代碼是否會驗證直接處理程序的請求?因爲那時我認爲這個代碼不會被執行。 – 2014-09-10 15:06:08
您好,先生,我使用VS2013和.Net FrameWork 4.5創建我的ASP.net網頁表單應用程序,但我的母版頁不包含此自動生成的代碼,我如何知道我的網站是否從CSRF安全? – 2015-04-29 09:20:53
伊恩·伊波利托答案這裏的C#版本:
public partial class SiteMaster : MasterPage
{
private const string AntiXsrfTokenKey = "__AntiXsrfToken";
private const string AntiXsrfUserNameKey = "__AntiXsrfUserName";
private string _antiXsrfTokenValue;
protected void Page_Init(object sender, EventArgs e)
{
// The code below helps to protect against XSRF attacks
var requestCookie = Request.Cookies[AntiXsrfTokenKey];
Guid requestCookieGuidValue;
if (requestCookie != null && Guid.TryParse(requestCookie.Value, out requestCookieGuidValue))
{
// Use the Anti-XSRF token from the cookie
_antiXsrfTokenValue = requestCookie.Value;
Page.ViewStateUserKey = _antiXsrfTokenValue;
}
else
{
// Generate a new Anti-XSRF token and save to the cookie
_antiXsrfTokenValue = Guid.NewGuid().ToString("N");
Page.ViewStateUserKey = _antiXsrfTokenValue;
var responseCookie = new HttpCookie(AntiXsrfTokenKey)
{
HttpOnly = true,
Value = _antiXsrfTokenValue
};
if (FormsAuthentication.RequireSSL && Request.IsSecureConnection)
{
responseCookie.Secure = true;
}
Response.Cookies.Set(responseCookie);
}
Page.PreLoad += master_Page_PreLoad;
}
protected void master_Page_PreLoad(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Set Anti-XSRF token
ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey;
ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty;
}
else
{
// Validate the Anti-XSRF token
if ((string)ViewState[AntiXsrfTokenKey] != _antiXsrfTokenValue
|| (string)ViewState[AntiXsrfUserNameKey] != (Context.User.Identity.Name ?? String.Empty))
{
throw new InvalidOperationException("Validation of Anti-XSRF token failed.");
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
是否需要使用上下文中的用戶標識進行驗證?當頁面之間的上下文繼續時,視圖狀態將保持該頁面的狀態。如果在驗證運行的時候身份更改(通過瀏覽多個選項卡),它將引發異常,因爲ViewState不會發生更改。 – Tristan 2017-12-21 19:47:31
任何代碼來說明如何做到這一點? – 2015-08-10 07:40:59
@ShekharPankaj請參閱[OWASP .NET安全備忘單](https://www.owasp.org/index.php/.NET_Security_Cheat_Sheet#ASP.NET_Web_Forms_Guidance)。確保你在整合之前瞭解它(即它保護你的東西,更重要的是它不能保護你)(http://security.stackexchange.com/q/59470))。 – tne 2015-12-14 11:48:56