写在前面

最近在摸索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. 信息安全系统设计基础实验一:Linux开发环境的配置和使用(20135234,20135229)

    http://www.cnblogs.com/mqy123/p/4968386.html

  2. vim环境设置和自动对齐

    只要在 /etc/vimrc中加上这两句就行了set autoindentset smartindent------------------------------------------------ ...

  3. GCC:条件判断中赋值语句和函数结尾时无返回值的警告

    有下面非常经典的一个字符串复制程序. test1.c #include <stdio.h> int main() { char str_t[]="This String come ...

  4. 总结一下工作中遇到的NPOI以及在ASP.NET MVC中的使用

    1.前言 相信大家在工作中经常要遇到一些导入导出Execl操作.学习贵在分享,分享使人快乐,园子里的前辈已经有很多好的文章,鄙人也是能力有限,在这里把这些好的文章总结,方便以后再工作中使用. NPOI ...

  5. EntityFramework中Mapper怎么定义联合主键?

    HasKey(m => new { m.StoreId, m.CarTypeId, m.CarLevel}) 用“new {}”联合主键以“,”分隔形式定义

  6. angular(常识)

    我觉得angularjs是前端框架,而jquery只是前端工具,这两个还是没有可比性的. 看知乎上关于jquery和angular的对比http://www.zhihu.com/question/27 ...

  7. asp.net网站安全常见问题与防范

    1:SQL 注入 2:XSS 3:CSRF 4:文件上传 1:SQL 注入 引起原因: 其实现在很多网站中都存在这种问题.就是程序中直接进行SQL语句拼接.可能有些读者不太明白. 下面通过一个登录时对 ...

  8. js中定时器的使用

    1.setInterval <!DOCTYPE html> <html> <head> <title>json</title> <sc ...

  9. Nginx下Redmine配置

    安装redmine依赖的所有ruby包 cd .. gem install bundler #注意是在网站根目录下执行 bundle install --without development tes ...

  10. 作业一_随笔3_调研Android的开发环境的发展演变

    调研某一移动应用/平台的开发环境的发展演变:Android 其实,一开始,我只知道,苹果手机用IOS系统,其他很多手机时候安卓系统.我百度知道Android开发主要是android studio和Ec ...