创建-跨站请求屏蔽,盗链屏蔽

来源:互联网 发布:千兆路由器 知乎 编辑:程序博客网 时间:2024/06/06 06:47
using System;using System.Web;using System.Web.Mvc;namespace FmallExternal.Helper.MvcWeb{    /// <summary>    /// 跨站请求屏蔽,盗链屏蔽    /// </summary>    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = false, AllowMultiple = false)]    public sealed class IsPostedFromThisSiteAttribute : ActionFilterAttribute    {        /// <summary>        /// 在执行操作方法后由 ASP.NET MVC 框架调用。        /// </summary>        /// <param name="filterContext">筛选器上下文。</param>        public override void OnActionExecuting(ActionExecutingContext filterContext)        {            if (filterContext != null && filterContext.HttpContext != null)            {                if (filterContext.HttpContext.Request.UrlReferrer == null)                    throw new System.Web.HttpException("Invalid submission");                if (!string.Equals(                        filterContext.HttpContext.Request.UrlReferrer.Host,                        filterContext.HttpContext.Request.Url.Host,                        StringComparison.CurrentCultureIgnoreCase))                    throw new System.Web.HttpException("This form wasn't submitted from this site!");            }        }    }}
0 0