利用AuthorizeAttribute属性简单避免 MVC 中的跨域攻击
来源:互联网 发布:机智交易软件 编辑:程序博客网 时间:2024/05/17 00:05
跨域攻击---自然来路页面和目标页面不在同一个域下,所以直接判断来路域和当前自己的域就可以了。
可以广泛应用于表单提交,ajax调用或者某些不想让用户直接输入网址看到的页面
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace Admin.MyAttribute
- {
- [AttributeUsage(AttributeTargets.All, Inherited = true)]
- public class CheckAuthority : AuthorizeAttribute
- {
- protected override bool AuthorizeCore(HttpContextBase httpContext)
- {
- bool Pass = true;
- Uri UrlReferrer = httpContext.Request.UrlReferrer;//获取来路
- if (UrlReferrer == null)
- {
- httpContext.Response.StatusCode = 401;//无权限状态码
- Pass = false;
- }
- else
- {
- Uri ThisUrl = httpContext.Request.Url;//当前请求的URL
- if (UrlReferrer.Authority != ThisUrl.Authority)
- {
- httpContext.Response.StatusCode = 401;//无权限状态码
- Pass = false;
- }
- }
- return Pass;
- }
- protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
- {
- base.HandleUnauthorizedRequest(filterContext);
- if (filterContext.HttpContext.Response.StatusCode == 401)
- filterContext.Result = new RedirectResult("/");
- }
- }
- }
- 调用方法
- [MyAttribute.CheckAuthority]
- public ActionResult Index()
- {
- return View();
- }
0 0
- 利用AuthorizeAttribute属性简单避免 MVC 中的跨域攻击
- 利用AuthorizeAttribute属性简单避免 MVC 中的跨域攻击
- 利用AuthorizeAttribute属性避免 MVC 中的跨域攻击
- c# mvc 自定义AuthorizeAttribute
- MVC用户权限管理AuthorizeAttribute
- MVC特性认证 AuthorizeAttribute 类
- MVC特性认证 AuthorizeAttribute 类
- MVC中使用AuthorizeAttribute身份验证
- AuthorizeAttribute
- mvc 3.0 自定义 AuthorizeAttribute 权限管理
- MVC中使用AuthorizeAttribute做身份验证操作
- MVC中使用AuthorizeAttribute做身份验证操作
- MVC 拦截器之授权 AuthorizeAttribute
- MVC自定义AuthorizeAttribute管理页面浏览权限
- MVC中使用AuthorizeAttribute做身份验证操作
- MVC 自定义AuthorizeAttribute实现权限管理
- MVC中使用AuthorizeAttribute做身份验证操作
- C#中避免SQL中的注入式攻击(winform)
- Redis常用命令
- 关于socket长连接的心跳包
- wireshark怎么抓包、wireshark抓包详细图文教程
- 基于cryptopp -- A Password Manager密码管理系统
- The attribute locations is undefined for the annotation type ContextConfiguration
- 利用AuthorizeAttribute属性简单避免 MVC 中的跨域攻击
- 在ListView中实现异步加载
- 后台定位上传的代码实践
- 设置启动页图
- 给MFC对话框控件添加工具提示Tips
- java web HttpClient 开发一个接口
- poj 1141(区间dp+打印路径)
- Yii之路(第八)
- 推送对账文件的3种