[asp.net mvc]自定义filter
写在前面
最近在摸索mvc,在app中的webview中嵌入h5应用,经常需要用到对cookie的读取操作。所以想到通过自定义的filter截取cookie,然后通过在action上面打特性的方式针对需要认证的action进行授权。
一个例子
简单的userInfo类
- public class UserInfo
- {
- public string Name { set; get; }
- public string Pwd { set; get; }
- }
自定义的filter
- public class CookieAuthAttribute : ActionFilterAttribute
- {
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- //当前action的参数名称
- const string actionParameter = "cookieUser";
- HttpContext context = HttpContext.Current;
- if (filterContext.ActionParameters.ContainsKey(actionParameter))
- {
- HttpCookie nameCookie = context.Request.Cookies["n"];
- HttpCookie pwdCookie = context.Request.Cookies["p"];
- filterContext.ActionParameters[actionParameter] = null;
- if (nameCookie != null && pwdCookie != null)
- {
- filterContext.ActionParameters[actionParameter] = new UserInfo() { Name = nameCookie.Value, Pwd = pwdCookie.Value };
- }
- }
- base.OnActionExecuting(filterContext);
- }
- }
注册filter
- public class FilterConfig
- {
- public static void RegisterGlobalFilters(GlobalFilterCollection filters)
- {
- filters.Add(new HandleErrorAttribute());
- filters.Add(new CookieAuthAttribute());
- }
- }
- public class MvcApplication : System.Web.HttpApplication
- {
- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- }
- }
在需要授权的action上面打特性标记
- public class HomeController : Controller
- {
- // GET: Home
- [CookieAuth]
- public ActionResult Index(UserInfo cookieUser)
- {
- if (cookieUser == null)
- {
- return new EmptyResult();
- }
- else
- {
- return View(cookieUser);
- }
- }
- public void Login()
- {
- HttpCookie nameCookie = new HttpCookie("n", "wolfy");
- HttpCookie pwdCookie = new HttpCookie("p", "");
- Response.Cookies.Add(nameCookie);
- Response.Cookies.Add(pwdCookie);
- }
- }
通过Login写入cookie,然后访问Index
结果
这样通过cookie来对要访问的action授权就简单多了,只需在需要认证的action上面打上特性 [CookieAuth]就可以了。
[asp.net mvc]自定义filter的更多相关文章
- Asp.net mvc自定义Filter简单使用
自定义Filter的基本思路是继承基类ActionFilterAttribute,并根据实际需要重写OnActionExecuting,OnActionExecuted,OnResultExecuti ...
- asp.net mvc 自定义pager封装与优化
asp.net mvc 自定义pager封装与优化 Intro 之前做了一个通用的分页组件,但是有些不足,从翻页事件和分页样式都融合在后台代码中,到翻页事件可以自定义,再到翻页和样式都和代码分离, 自 ...
- ASP.NET MVC 自定义路由中几个需要注意的小细节
本文主要记录在ASP.NET MVC自定义路由时,一个需要注意的参数设置小细节. 举例来说,就是在访问 http://localhost/Home/About/arg1/arg2/arg3 这样的自定 ...
- Asp.net Mvc 自定义Session (二)
在 Asp.net Mvc 自定义Session (一)中我们把数据缓存工具类写好了,今天在我们在这篇把 剩下的自定义Session写完 首先还请大家跟着我的思路一步步的来实现,既然我们要自定义Ses ...
- Asp.net mvc 自定义全局的错误事件HandleErrorAttribute无效
Asp.net mvc 自定义全局的错误事件HandleErrorAttribute,结果无效, 原因: 1.没有在RegisterGlobalFilters 里面添加或者你要的位置添加. 2.你把这 ...
- ASP.NET MVC自定义验证Authorize Attribute(包含cookie helper)
前几天Insus.NET有在数据库实现过对某一字段进行加密码与解密<使用EncryptByPassPhrase和DecryptByPassPhrase对MS SQLServer某一字段时行加密和 ...
- ASP.NET MVC 自定义Razor视图WorkContext
概述 1.在ASP.NET MVC项目开发的过程中,我们经常需要在cshtml的视图层输出一些公用信息 比如:页面Title.服务器日期时间.页面关键字.关键字描述.系统版本号.资源版本号等 2.普通 ...
- 反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) C#中缓存的使用 C#操作redis WPF 控件库——可拖动选项卡的TabControl 【Bootstrap系列】详解Bootstrap-table AutoFac event 和delegate的分别 常见的异步方式async 和 await C# Task用法 c#源码的执行过程
反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) 背景介绍: 为了平衡社区成员的贡献和索取,一起帮引入了帮帮币.当用户积分(帮帮点)达到一定数额之后,就会“掉落”一定数量的“帮帮 ...
- asp.net MVC 自定义模型绑定 从客户端中检测到有潜在危险的 Request.QueryString 值
asp.net mvc 自定义模型绑定 有潜在的Requset.Form 自定义了一个模型绑定器.前端会传过来一些敏感字符.调用bindContext. valueProvider.GetValue( ...
随机推荐
- Objective-c复制对象的概念
- Caffe学习系列(15):计算图片数据的均值
图片减去均值后,再进行训练和测试,会提高速度和精度.因此,一般在各种模型中都会有这个操作. 那么这个均值怎么来的呢,实际上就是计算所有训练样本的平均值,计算出来后,保存为一个均值文件,在以后的测试中, ...
- css 兼容
color:#0000FF\9; ;/*ie6,ie7,ie8*/ *color:#FFFF00;/*ie7*/ _color:#FF0000;/*ie6*/ body:nth-of-type(1) ...
- Android一个大众化的设置界面
布局代码如下: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:an ...
- 把字符串添加到HashMap中
&ZhuoTai_Name=205&NoSongDanDish=0&OrderZhuoTai_ID=aca87b77797e4c859a53c228471a2636&Z ...
- gcc学习
gcc学习 预处理:gcc –E xxx.c –o xxx.i;产生预处理过的C原始程序 编 译:gcc –S xxx.i –o xxx.s;产生汇编语言原始程序 汇 编:gcc –c xxx.s – ...
- 20145208 实验三 Java面向对象程序设计
20145208 实验三 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步 ...
- 第二章 Js函数
函数的定义二种定义 ①function myfunc () { console("hello"); }; ②var myfunc = function () { console ...
- 今天学习到的关于mysql数据库的linux命令
1. 登录mysql数据库: mysql -uroot -p 2.安装会提示的mysql的数据库软件:mycli sudo apt-get install mycli 3.安装依赖包: sudo ap ...
- C语言和数据结构的书单-再次推荐
一.推荐专业书单: 1) C语言方面: n 明解C语言——适合初学者 豆瓣链接:https://book.douban.com/subject/23779374/ 推荐理由:< ...