朴素的标题:MVC中权限管理实践
2016-01-13 18:32
253 查看
基于MVC的web项目最好的权限控制方式我认为是对Action的控制,实现思路记录于此,权限管理分成两个部分授权、认证。
[b]一、授权[/b]
1、读取当前项目中的所有需要控制的Action
View Code
2、在 Global中注册过滤器
3、给一些不需要授权的Action加上特性[AllowAnonymous],当然也可以自定义其他的特性
由此基本完成,也可以下载源码参考下。
[b]一、授权[/b]
1、读取当前项目中的所有需要控制的Action
public override void OnActionExecuting(ActionExecutingContext filterContext) { //获取当前用户所拥有的权限 List<ActionInfo> right = new List<ActionInfo>() { new ActionInfo() { Action = "A1", Controller = "RBAC.Controllers.AController" }, new ActionInfo() { Action = "A2", Controller = "RBAC.Controllers.AController" } }; //白名单 var whiteAction = GetWhiteAction(); //获取用户要请求的页面 ActionInfo context = new ActionInfo() { Action = filterContext.ActionDescriptor.ActionName, Controller = filterContext.Controller.ToString() }; //判断是否有权访问 if (!(whiteAction.Any(r => r.Action.Equals(context.Action, StringComparison.CurrentCultureIgnoreCase) && r.Controller.Equals(context.Controller, StringComparison.CurrentCultureIgnoreCase)) || right.Any(r => r.Action.Equals(context.Action, StringComparison.CurrentCultureIgnoreCase) && r.Controller.Equals(context.Controller, StringComparison.CurrentCultureIgnoreCase)))) { filterContext.HttpContext.Response.Redirect("/Main/Msg"); } }
View Code
2、在 Global中注册过滤器
GlobalFilters.Filters.Add(new AuthFilter());
3、给一些不需要授权的Action加上特性[AllowAnonymous],当然也可以自定义其他的特性
[AllowAnonymous] public ActionResult Msg() { return View(); }
由此基本完成,也可以下载源码参考下。
相关文章推荐
- WEBLOGIC 启用SNMP
- RadioButton间距问题
- [已解决]linux安装pillow报错: ValueError: jpeg is required unless explicitly disabled using --disable-jpeg, aborting
- Notification窗体推送功能
- Atitit.导出excel报表的设计与实现java .net php 总
- Android 仿转转商品详情页面图片消失方式
- Atitit.导出excel报表的设计与实现java .net php 总
- MySQL数据库查询优化
- windows上自动化脚本的一次探索
- Atitit.导出excel报表的设计与实现java .net php 总结
- DOM中元素节点、属性节点、文本节点的理解13.3
- Android View系统解析(下)
- Linux中内存buffer和cache的区别
- 关于mybatis 提示的错误 Type interface com.souvi.ibatis.xxxMapper is not known to the Ma
- CentOS下使用pip安装scipy
- 有用的Python代码片段
- ActiveMQ(5.10.0) - Destination-level authorization
- servlet学习记录
- Android View系统解析(上)
- Linux内核TCP/IP参数分析与调优