我已经使用Visual Studio 2013创建了ASP.Net Web窗体应用程序,并且正在使用.NET Framework 4.5。我想确保我的网站不受跨站请求伪造(CSRF)的影响,我发现有很多文章都在谈论如何在MVC应用程序上实现此功能,但是很少有文章谈论Web表单。关于this StackOverflow question,有一条评论指出
"This is an old question, but the latest Visual Studio 2012 ASP.NET template for web forms includes anti-CSRF code baked into the master page. If you don't have the templates, here's the code it generates:..."
我的母版页不包含该答案中提到的代码。它真的包含在新应用程序中吗?如果没有,添加它的最佳方法是什么?
最佳答案
您可以尝试以下方法。在Web窗体中添加:
<%= System.Web.Helpers.AntiForgery.GetHtml() %>
这将添加一个隐藏字段和一个cookie。因此,如果您填写一些表单数据并将其发布回服务器,则需要简单检查一下:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
AntiForgery.Validate(); // throws an exception if anti XSFR check fails.
}
如果反XSFR检查失败,则
AntiForgery.Validate();
引发异常。
关于asp.net - 防止ASP.NET Web表单中的跨站点请求伪造(CSRF)攻击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29939566/