原文发布时间为: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 防止伪造提交的更多相关文章

  1. MVC防止xss攻击 ——Html.AntiForgeryToken的AJAX提交

    1.在Html表单里面使用了@Html.AntiForgeryToken()就可以阻止CSRF攻击. 2.相应的我们要在Controller中也要加入[ValidateAntiForgeryToken ...

  2. 快速学会使用Fiddler抓包 截包伪造提交包

    1.Fiddler介绍 Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑,移动设备和互联网之间的http通讯,设置断点,查看所有的"进出"Fiddler的 ...

  3. Java HttpClient伪造请求之简易封装满足HTTP以及HTTPS请求

    HttpClient简介 HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 jav ...

  4. [原创]CI持续集成系统环境---部署gerrit环境完整记录

    开发同事提议在线上部署一套gerrit代码审核环境,不用多说,下面就是自己部署gerrit的操作记录. 提前安装好java环境,mysql环境,nginx环境 测试系统:centos6.5 下载下面三 ...

  5. Bypass Preventing CSRF

    CSRF在过去的n年(n>2)一直都火,在bh/defcon/owasp等会议上多次探讨CSRF的攻防[具体你可以看看以往的那些pp].前 段时间PLAYHACK.net上发表了一个总结性的pp ...

  6. WordPress wp-admin/includes/post.php脚本安全漏洞

    漏洞名称: WordPress wp-admin/includes/post.php脚本安全漏洞 CNNVD编号: CNNVD-201309-168 发布时间: 2013-09-13 更新时间: 20 ...

  7. Django学习---原生ajax

    Ajax 原生ajax Ajax主要就是使用 [XmlHttpRequest]对象来完成请求的操作,该对象在主流浏览器中均存在(除早起的IE),Ajax首次出现IE5.5中存在(ActiveX控件). ...

  8. 数据据操作 tp5

    数据库操作-DB类 学习手册 数据库配置 注意1:在TP里面,可以在模块下面单独的建立一个database.php配置文件,代表这个模块就使用配置的这个数据库 注意2:我们可以在config.php里 ...

  9. PostgreSQL Replication之第二章 理解PostgreSQL的事务日志(1)

    在前面的章节中,我们已经理解了各种复制概念.这不仅仅是一个为了接下来将要介绍的东西而增强您的意识的理论概述,还将为您介绍大体的主题. 在本章,我们将更加接近实际的解决方案,并了解PostgreSQL内 ...

随机推荐

  1. [BZOJ] 3875: [Ahoi2014&Jsoi2014]骑士游戏

    设\(f[x]\)为彻底杀死\(x\)号怪兽的代价 有转移方程 \[ f[x]=min\{k[x],s[x]+\sum f[v]\} \] 其中\(v\)是\(x\)通过普通攻击分裂出的小怪兽 这个东 ...

  2. PyCharm 2018.1 软件汉化

    下载汉化包 链接: https://pan.baidu.com/s/1buLFINImW_3cNzP8HsB4cA 密码: fqpu 安装汉化包 找到pycharm安装目录 直接把刚刚下载的汉化包复制 ...

  3. 快速搭建FTP服务

    Linux下ftp服务可以通过搭建vsftpd服务来实现,以CentOS为例,首先查看系统中是否安装了vsftpd,可以通过执行命令 rpm -qa | grep vsftpd 来查看是否安装相应的包 ...

  4. Class:向传统类模式转变的构造函数

    前言 JS基于原型的'类',一直被转行前端的码僚们大呼惊奇,但接近传统模式使用class关键字定义的出现,却使得一些前端同行深感遗憾而纷纷留言:"还我独特的JS"."净搞 ...

  5. 二 python并发编程之多进程-理论

    一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): egon在一个时间段内有很多任务要做:python备课的任务,写书的任 ...

  6. nowcoder N约数个数

    n的约数个数 题目:t次询问,每次给你一个数n,求在[1,n]内约数个数最多的数的约数个数 数据:对于100%的数据,t <= 500 , 1 <= n <= 10000000000 ...

  7. 笔记-python-tutorial-8.errors and exceptions

    笔记-python-tutorial-8.errors and exceptions 1.      errors and exceptions 1.1.    syntax errors >& ...

  8. poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7825   Accepted: 3068 Descri ...

  9. 矩阵儿快速幂 - POJ 3233 矩阵力量系列

    不要管上面的标题的bug 那是幂的意思,不是力量... POJ 3233 Matrix Power Series 描述 Given a n × n matrix A and a positive in ...

  10. 通过APP,网页打开手机客户端QQ

    以下内容为转载,原帖子 http://m.blog.csdn.net/blog/qduningning/40587099 在浏览器中可以通过JS代码打开QQ并弹出聊天界面,一般作为客服QQ使用.而在移 ...