Html.AntiForgeryToken 防止伪造提交
原文发布时间为:2011-05-03 —— 来源于本人的百度文章 [由搬家工具导入]
In this tutorial, I am not going to discuss the concept in-depth since they have done such a fantastic job. Instead, I want to show how you can easily incorporate the Html.AntiForgeryToken HtmlHelper Method and [ValidateAntiForgeryToken] Attribute in the sample code from our first meeting:Introduction to ASP.NET MVC Screencast and Sample Code.
This is really a two-step process:
Add the [ValidateAntiForgeryToken] Attribute to any Post Action Methods.Add the Html.AntiForgeryToken() HtmlHelper Method to any forms posting back to the website.
Let's just do this to the Edit Action Method in the ContactsController in this tutorial.
[ValidateAntiForgeryToken] Attribute
The ValidateAntiForgeryToken Attribute in the ASP.NET MVC Framework is an IAuthorizationFilter which is guaranteed to run before any other filters. You add it to your post Action Methods as such:
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
publicActionResultEdit(Contactcontact)
{
try
{
if (!ModelState.IsValid)
returnView(contact);
_db.Contacts.Attach(contact, true);
_db.SubmitChanges();
returnRedirectToAction("List");
}
catch
{
returnView(contact);
}
}
The ValidateAntiForgeryToken Attribute does some voodoo magic in the background. It checks to see that the cookie and hidden form field left by the Html.AntiForgeryToken() HtmlHelper essentially exists and match. If they do not exist or match, it throws an HttpAntiForgeryException:
You can obviously clean that exception up, but the important point is that if the cookie and form field do not exist or match the action method is not executed for security reasons. This is a good thing!
Html.AntiForgeryToken HtmlHelper Method
As mentioned before, we need to add the Html.AntiForgeryToken Method to the forms so that a cookie is added on the client and a hidden form field is added to the form itself. This is as simple as:
You will notice the hidden form field in the HTML source:
The Html.AntiForgeryToken Method will also add a cookie on your machine matching the same value in the hidden form field. It is the cookie and the hidden form field value that the ValidateAntiForgeryToken Attribute is checking.
Conclusion
Pretty simple way to protect your website against Cross-Site Request Forgery Attacks in the ASP.NET MVC Framework.
You can add this code yourself by downloading the Tampa MVC Developer Group Screencast and sample code based on our first meeting.
Html.AntiForgeryToken 防止伪造提交的更多相关文章
- MVC防止xss攻击 ——Html.AntiForgeryToken的AJAX提交
1.在Html表单里面使用了@Html.AntiForgeryToken()就可以阻止CSRF攻击. 2.相应的我们要在Controller中也要加入[ValidateAntiForgeryToken ...
- 快速学会使用Fiddler抓包 截包伪造提交包
1.Fiddler介绍 Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑,移动设备和互联网之间的http通讯,设置断点,查看所有的"进出"Fiddler的 ...
- Java HttpClient伪造请求之简易封装满足HTTP以及HTTPS请求
HttpClient简介 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 jav ...
- [原创]CI持续集成系统环境---部署gerrit环境完整记录
开发同事提议在线上部署一套gerrit代码审核环境,不用多说,下面就是自己部署gerrit的操作记录. 提前安装好java环境,mysql环境,nginx环境 测试系统:centos6.5 下载下面三 ...
- Bypass Preventing CSRF
CSRF在过去的n年(n>2)一直都火,在bh/defcon/owasp等会议上多次探讨CSRF的攻防[具体你可以看看以往的那些pp].前 段时间PLAYHACK.net上发表了一个总结性的pp ...
- WordPress wp-admin/includes/post.php脚本安全漏洞
漏洞名称: WordPress wp-admin/includes/post.php脚本安全漏洞 CNNVD编号: CNNVD-201309-168 发布时间: 2013-09-13 更新时间: 20 ...
- Django学习---原生ajax
Ajax 原生ajax Ajax主要就是使用 [XmlHttpRequest]对象来完成请求的操作,该对象在主流浏览器中均存在(除早起的IE),Ajax首次出现IE5.5中存在(ActiveX控件). ...
- 数据据操作 tp5
数据库操作-DB类 学习手册 数据库配置 注意1:在TP里面,可以在模块下面单独的建立一个database.php配置文件,代表这个模块就使用配置的这个数据库 注意2:我们可以在config.php里 ...
- PostgreSQL Replication之第二章 理解PostgreSQL的事务日志(1)
在前面的章节中,我们已经理解了各种复制概念.这不仅仅是一个为了接下来将要介绍的东西而增强您的意识的理论概述,还将为您介绍大体的主题. 在本章,我们将更加接近实际的解决方案,并了解PostgreSQL内 ...
随机推荐
- 【计数】cf223C. Partial Sums
考试时候遇到这种题只会找规律 You've got an array a, consisting of n integers. The array elements are indexed from ...
- nodejs源码—初始化
概述 相信很多的人,每天在终端不止一遍的执行着node这条命令,对于很多人来说,它就像一个黑盒,并不知道背后到底发生了什么,本文将会为大家揭开这个神秘的面纱,由于本人水平有限,所以只是讲一个大概其,主 ...
- cmf5分页相关
//分页配置在app/config.php 'paginate' => [ 'type' => '\cmf\paginator\Bootstrap', 'var_page' => ' ...
- Android系统中标准Intent的使用
Android系统用于Activity的标准Intent 1.根据联系人ID显示联系人信息= Intent intent=new Intent(); intent.setAction(Intent.A ...
- Matlab数据转化至python端,并写入数据库
因工作原因,一些获取的行业数据以已知的结构体存储在.mat文件中, 现需要将其存储在数据库中并且能够灵活调用至python dataframe里进行操作 原数据的一个例子如下 目标如上: 然后是转化代 ...
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- B1007 素数对猜想
B1007 素数对猜想 让我们定义\(d_n\)为:\(d_n =p_{n+1}−p_n\),其中\(p_i\)是第i个素数.显然有\(d_1=1\),且对于n>1有\(d_n\)是偶数.&qu ...
- 2015-2016 Northwestern European Regional Contest (NWERC 2015)
训练时间:2019-04-05 一场读错三个题,队友恨不得手刃了我这个坑B. A I J 简单,不写了. C - Cleaning Pipes (Gym - 101485C) 对于有公共点的管道建边, ...
- 动态规划:HDU-2542-0-1背包问题:饭卡
解题心得: 这题就是一个简单的0-1背包问题,只不过加了一系列的限制.可以想办法消去限制,直接转换成0-1背包问题的模板形式. 需要注意的几个点:首先对于剩余的5元钱的处理可以直接在总的钱数上将5减去 ...
- 读书笔记jvm探秘之二: 对象创建
对象是面向对象设计语言无法回避的东西,可见其重要性,JAVA的对象相较于C++来说,不算很复杂,但是我们看到一句话背后往往有很多东西值得探讨(NEW关键字). 对象如何被创建? 首先一句简单的NEW语 ...