写在前面

最近在摸索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的更多相关文章

  1. Asp.net mvc自定义Filter简单使用

    自定义Filter的基本思路是继承基类ActionFilterAttribute,并根据实际需要重写OnActionExecuting,OnActionExecuted,OnResultExecuti ...

  2. asp.net mvc 自定义pager封装与优化

    asp.net mvc 自定义pager封装与优化 Intro 之前做了一个通用的分页组件,但是有些不足,从翻页事件和分页样式都融合在后台代码中,到翻页事件可以自定义,再到翻页和样式都和代码分离, 自 ...

  3. ASP.NET MVC 自定义路由中几个需要注意的小细节

    本文主要记录在ASP.NET MVC自定义路由时,一个需要注意的参数设置小细节. 举例来说,就是在访问 http://localhost/Home/About/arg1/arg2/arg3 这样的自定 ...

  4. Asp.net Mvc 自定义Session (二)

    在 Asp.net Mvc 自定义Session (一)中我们把数据缓存工具类写好了,今天在我们在这篇把 剩下的自定义Session写完 首先还请大家跟着我的思路一步步的来实现,既然我们要自定义Ses ...

  5. Asp.net mvc 自定义全局的错误事件HandleErrorAttribute无效

    Asp.net mvc 自定义全局的错误事件HandleErrorAttribute,结果无效, 原因: 1.没有在RegisterGlobalFilters 里面添加或者你要的位置添加. 2.你把这 ...

  6. ASP.NET MVC自定义验证Authorize Attribute(包含cookie helper)

    前几天Insus.NET有在数据库实现过对某一字段进行加密码与解密<使用EncryptByPassPhrase和DecryptByPassPhrase对MS SQLServer某一字段时行加密和 ...

  7. ASP.NET MVC 自定义Razor视图WorkContext

    概述 1.在ASP.NET MVC项目开发的过程中,我们经常需要在cshtml的视图层输出一些公用信息 比如:页面Title.服务器日期时间.页面关键字.关键字描述.系统版本号.资源版本号等 2.普通 ...

  8. 反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) C#中缓存的使用 C#操作redis WPF 控件库——可拖动选项卡的TabControl 【Bootstrap系列】详解Bootstrap-table AutoFac event 和delegate的分别 常见的异步方式async 和 await C# Task用法 c#源码的执行过程

    反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑)   背景介绍: 为了平衡社区成员的贡献和索取,一起帮引入了帮帮币.当用户积分(帮帮点)达到一定数额之后,就会“掉落”一定数量的“帮帮 ...

  9. asp.net MVC 自定义模型绑定 从客户端中检测到有潜在危险的 Request.QueryString 值

    asp.net mvc 自定义模型绑定 有潜在的Requset.Form 自定义了一个模型绑定器.前端会传过来一些敏感字符.调用bindContext. valueProvider.GetValue( ...

随机推荐

  1. [CareerCup] 13.3 Virtual Functions 虚函数

    13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...

  2. post数据

    http://cnodejs.org/topic/4f16442ccae1f4aa2700104d http://www.cnblogs.com/pingfan1990/p/4701355.html ...

  3. 15.C#回顾及匿名类型(八章8.1-8.5)

    今天的篇幅应该会很长,除了回顾前面学的一些,还有写一些关于匿名类型的相关知识,总体上对后续的学习很有帮助,学好了,后面更容易理解,不明白的,那就前面多翻几次,看多了总是会理解的.那么,进入正题吧. 自 ...

  4. Quartz.net的cron表达式

    写在前面 前面有一篇文章用到了quartz.net,在设置定时时间的时候,使用了cron表达式,这里记录几种常见设置方式,方便对照使用. 详情 在这篇文章:Quartz.Net在windows服务中的 ...

  5. 国内公共DNS

    DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串.通过主机名,最终 ...

  6. Future模式

    Future模式简介 Future模式有点类似于网上购物,在你购买商品,订单生效之后,你可以去做自己的事情,等待商家通过快递给你送货上门.Future模式就是,当某一程序提交请求,期望得到一个答复.但 ...

  7. openvpn的介绍和搭建过程

    本文摘自:http://www.linuxidc.com/Linux/2012-01/51702.htm,在这只是为了做个笔记使用

  8. grunt安装_

    grunt_构建WEBJS程序框架,. package.json是文件配置 ====〉〉〉〉 在Gruntfile.js里面会引用到. //目录下直接放node的东西 ,比如: node_module ...

  9. du 命令

    Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 1.命令格式: du [选项][文件] 2.命令功能 ...

  10. 【HDU 5578】Friendship of Frog

    题 题意 求相同字母最近距离 分析 用数组保存各个字母最后出现的位置,维护最小距离. 代码 #include <cstdio> int c[30],n,p,a,minl; char ch; ...