ajax中如何使用AntiForgeryToken防止CSRF攻击

发布时间:2021-09-14 17:23:23 作者:小新
来源:亿速云 阅读:97

小编给大家分享一下ajax中如何使用AntiForgeryToken防止CSRF攻击,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

经常看到在项目中ajax post数据到服务器不加防伪标记,造成CSRF攻击

在Asp.net Mvc里加入防伪标记很简单在表单中加入Html.AntiForgeryToken()即可。

Html.AntiForgeryToken()会生成一对加密的字符串,分别存放在Cookies 和 input 中。

我们在ajax post中也带上AntiForgeryToken

@model WebApplication1.Controllers.Person
@{
 ViewBag.Title = "Index";
}
<h3>Index</h3>
<form id="form1">
 <div class="form-horizontal">
  <h5>Persen</h5>
  <hr />
  @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  <div class="form-group">
   @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
    @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
   </div>
  </div>
  <div class="form-group">
   @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
    @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" })
   </div>
  </div>
  <div class="form-group">
   <div class="col-md-offset-2 col-md-10">
    <input type="button" id="save" value="Create" class="btn btn-default" />
   </div>
  </div>
 </div>
</form>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script type="text/javascript">
 $(function () {
  //var token = $('[name=__RequestVerificationToken]');
  //获取防伪标记
  var token = $('@Html.AntiForgeryToken()').val();
  var headers = {};
  //防伪标记放入headers
  //也可以将防伪标记放入data
  headers["__RequestVerificationToken"] = token;
  $("#save").click(function () {
   $.ajax({
    type: 'POST',
    url: '/Home/Index',
    cache: false,
    headers: headers,
    data: { Name: "yangwen", Age: "1" },
    success: function (data) {
     alert(data)
    },
    error: function () {
     alert("Error")
    }
   });
  })
 })
</script>

放在cookies里面的加密字符串

控制器中代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
namespace WebApplication1.Controllers
 {
 public class HomeController : Controller
  {
  public ActionResult Index()
   {
   return View();
   }
  [HttpPost]
  [MyValidateAntiForgeryToken]
  public ActionResult Index(Person p)
   {
   return Json(true, JsonRequestBehavior.AllowGet);
   }
  }
 public class Person
  {
  public string Name { get; set; }
  public int Age { get; set; }
  }
 public class MyValidateAntiForgeryToken : AuthorizeAttribute
  {
  public override void OnAuthorization(AuthorizationContext filterContext)
   {
   var request = filterContext.HttpContext.Request;
   if (request.HttpMethod == WebRequestMethods.Http.Post)
    {  
    if (request.IsAjaxRequest())
     {
     var antiForgeryCookie = request.Cookies[AntiForgeryConfig.CookieName];
     var cookieValue = antiForgeryCookie != null
      ? antiForgeryCookie.Value
      : null;
     //从cookies 和 Headers 中 验证防伪标记
     //这里可以加try-catch
     AntiForgery.Validate(cookieValue, request.Headers["__RequestVerificationToken"]);
     }
    else
     {
     new ValidateAntiForgeryTokenAttribute()
      .OnAuthorization(filterContext);
     }
    }
   }
  }
 }

这里注释掉ajax中防伪标记在请求

$("#save").click(function () {
 $.ajax({
  type: 'POST',
  url: '/Home/Index',
  cache: false,
 //  headers: headers,
  data: { Name: "yangwen", Age: "1" },
  success: function (data) {
   alert(data)
  },
  error: function () {
   alert("Error")
  }
 });
})

默认返回500的状态码。

这里修改ajax中的防伪标记

  $(function () {
 //var token = $('[name=__RequestVerificationToken]');
 //获取防伪标记
 var token = $('@Html.AntiForgeryToken()').val();
 var headers = {};
 //防伪标记放入headers
 //也可以将防伪标记放入data
 headers["__RequestVerificationToken"] = token+11111111111111111111111111111111111;
 $("#save").click(function () {
  $.ajax({
   type: 'POST',
   url: '/Home/Index',
   cache: false,
    headers: headers,
   data: { Name: "yangwen", Age: "1" },
   success: function (data) {
    alert(data)
   },
   error: function () {
    alert("Error")
   }
  });
 })
})

也是500的状态码。

以上是“ajax中如何使用AntiForgeryToken防止CSRF攻击”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. 在Django中如何预防CSRF攻击
  2. 如何使用django中间件django.middleware.csrf.CsrfViewMiddleware防止csrf攻击

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ajax csrf

上一篇:Ajax方式如何实现定期更新页面某块内容

下一篇:HTML5的垃圾分类系统代码实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》