You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HandlerValidateAntiForgeryTokenAttribute.cs 1.5 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Net;
  2. using System.Web.Helpers;
  3. using System.Web.Mvc;
  4. namespace Learun.Application.Web
  5. {
  6. /// <summary>
  7. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  8. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  9. /// 创建人:陈彬彬
  10. /// 日 期:2017.03.08
  11. /// 描 述:防伪验证
  12. /// </summary>
  13. public class HandlerValidateAntiForgeryTokenAttribute:AuthorizeAttribute
  14. {
  15. /// <summary>
  16. /// 拦截器
  17. /// </summary>
  18. /// <param name="filterContext">http上下文</param>
  19. public override void OnAuthorization(AuthorizationContext filterContext)
  20. {
  21. var request = filterContext.HttpContext.Request;
  22. if (request.HttpMethod == WebRequestMethods.Http.Post)
  23. {
  24. if (request.IsAjaxRequest())
  25. {
  26. var antiForgeryCookie = request.Cookies[AntiForgeryConfig.CookieName];
  27. var cookieValue = antiForgeryCookie != null
  28. ? antiForgeryCookie.Value
  29. : null;
  30. //从cookies 和 Headers 中 验证防伪标记
  31. //这里可以加try-catch
  32. AntiForgery.Validate(cookieValue, request.Headers["__RequestVerificationToken"]);
  33. }
  34. else
  35. {
  36. new ValidateAntiForgeryTokenAttribute().OnAuthorization(filterContext);
  37. }
  38. }
  39. }
  40. }
  41. }