的关键方法是在MVC源代码:GetAntiForgeryTokenAndSetCookie
这将创建一个名为AntiForgeryData
内部密封类的一个实例。
将实例序列化为cookie“__RequestVerificationToken_”+应用程序路径的基础64编码版本。
AntiForgeryData
的同一个实例被序列化为一个隐藏的输入。
的AntiForgeryData
的独特之处是使用了一个RNGCryptoServiceProvider.GetBytes()
所有这一切都可以在一个页面的WebForms是伪造的,唯一的凌乱位是隐藏密封类的序列化。不幸的是,关键方法(GetAntiForgeryTokenAndSetCookie
)依赖于ViewContext.HttpContext.Request
来获取cookie,而WebForm需要使用HttpContext.Current.Request
来代替。
更新
没有太多的测试和大量的代码,但我想我已经有一点思考破解这个。当我使用反射我已经离开上述注释掉相当于行:
using System;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
/// <summary>Utility to provide MVC anti forgery tokens in WebForms pages</summary>
public class WebFormAntiForgery
{
/// <summary>Create an anti forgery token in a WebForms page</summary>
/// <returns>The HTML input and sets the cookie</returns>
public static string AntiForgeryToken()
{
string formValue = GetAntiForgeryTokenAndSetCookie();
// string fieldName = AntiForgeryData.GetAntiForgeryTokenName(null);
var mvcAssembly = typeof(HtmlHelper).Assembly;
var afdType = mvcAssembly.GetType("System.Web.Mvc.AntiForgeryData");
string fieldName = Convert.ToString(afdType.InvokeMember(
"GetAntiForgeryTokenName",
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.InvokeMethod,
null,
null,
new object[] { null }));
TagBuilder builder = new TagBuilder("input");
builder.Attributes["type"] = "hidden";
builder.Attributes["name"] = fieldName;
builder.Attributes["value"] = formValue;
return builder.ToString(TagRenderMode.SelfClosing);
}
static string GetAntiForgeryTokenAndSetCookie()
{
var mvcAssembly = typeof(HtmlHelper).Assembly;
var afdType = mvcAssembly.GetType("System.Web.Mvc.AntiForgeryData");
// new AntiForgeryDataSerializer();
var serializerType = mvcAssembly.GetType("System.Web.Mvc.AntiForgeryDataSerializer");
var serializerCtor = serializerType.GetConstructor(new Type[0]);
object serializer = serializerCtor.Invoke(new object[0]);
// string cookieName = AntiForgeryData.GetAntiForgeryTokenName(HttpContext.Current.Request.ApplicationPath);
string cookieName = Convert.ToString(afdType.InvokeMember(
"GetAntiForgeryTokenName",
BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.InvokeMethod,
null,
null,
new object[] { HttpContext.Current.Request.ApplicationPath }));
// AntiForgeryData cookieToken;
object cookieToken;
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (cookie != null)
{
// cookieToken = Serializer.Deserialize(cookie.Value);
cookieToken = serializerType.InvokeMember("Deserialize", BindingFlags.InvokeMethod, null, serializer, new object[] { cookie.Value });
}
else
{
// cookieToken = AntiForgeryData.NewToken();
cookieToken = afdType.InvokeMember(
"NewToken",
BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod,
null,
null,
new object[0]);
// string cookieValue = Serializer.Serialize(cookieToken);
string cookieValue = Convert.ToString(serializerType.InvokeMember("Serialize", BindingFlags.InvokeMethod, null, serializer, new object[] { cookieToken }));
var newCookie = new HttpCookie(cookieName, cookieValue) { HttpOnly = true };
HttpContext.Current.Response.Cookies.Set(newCookie);
}
// AntiForgeryData formToken = new AntiForgeryData(cookieToken)
// {
// CreationDate = DateTime.Now,
// Salt = salt
// };
var ctor = afdType.GetConstructor(new Type[] { afdType });
object formToken = ctor.Invoke(new object[] { cookieToken });
afdType.InvokeMember("CreationDate", BindingFlags.SetProperty, null, formToken, new object[] { DateTime.Now });
afdType.InvokeMember("Salt", BindingFlags.SetProperty, null, formToken, new object[] { null });
// string formValue = Serializer.Serialize(formToken);
string formValue = Convert.ToString(serializerType.InvokeMember("Serialize", BindingFlags.InvokeMethod, null, serializer, new object[] { formToken }));
return formValue;
}
}
用法,然后类似MVC:
WebFormAntiForgery.AntiForgeryToken()
它创建相同的Cookie和相同的HTML作为MVC方法。
我并没有打扰盐和域的方法,但他们会很容易添加。
我有同样的问题 - 一个传统的WebForms页面,需要发布到一个MVC动作与'AntiForgeryToken'。我想将'Html.AntiForgeryToken()'添加到WebForms页面,而不用在MVC中重写它。 – Keith 2010-03-31 13:22:26