ASP.NET MVC4 权限验证
在ASP.NET MVC4 中继承ActionFilterAttribute 类,重写OnActionExecuting方法
/// <summary>
/// 权限拦截
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class PermissionFilterAttribute : ActionFilterAttribute
{
/// <summary>
/// 权限拦截
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//权限拦截是否忽略
bool IsIgnored = false;
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
var path = filterContext.HttpContext.Request.Path.ToLower();
//获取当前配置保存起来的允许页面
IList<string> allowPages = ConfigSettings.GetAllAllowPage();
foreach (string page in allowPages)
{
if (page.ToLower() == path)
{
IsIgnored = true;
break;
}
}
if (IsIgnored)
return;
//接下来进行权限拦截与验证
object[] attrs = filterContext.ActionDescriptor.GetCustomAttributes(typeof(ViewPageAttribute), true);
var isViewPage = attrs.Length == ;//当前Action请求是否为具体的功能页 if (this.AuthorizeCore(filterContext) == false)//根据验证判断进行处理
{
//注:如果未登录直接在URL输入功能权限地址提示不是很友好;如果登录后输入未维护的功能权限地址,那么也可以访问,这个可能会有安全问题
if (isViewPage == true)
{
//跳转到登录页面
filterContext.RequestContext.HttpContext.Response.Redirect("~/Admin/Manage/UserLogin");
}
else
{
object[] attrsUIException = filterContext.ActionDescriptor.GetCustomAttributes(typeof(LigerUIExceptionResultAttribute), true);
if (attrsUIException.Length == )
{
filterContext.Result = new FormatJsonResult() { IsError=true, Data=null,Message="您没有权限执行此操作!" };//功能权限弹出提示框
}
else filterContext.RequestContext.HttpContext.Response.Redirect("~/Admin/Manage/Error");
}
}
}
/// <summary>
/// [Anonymous标记]验证是否匿名访问
/// </summary>
/// <param name="filterContext"></param>
/// <returns></returns>
public bool CheckAnonymous(ActionExecutingContext filterContext)
{
//验证是否是匿名访问的Action
object[] attrsAnonymous = filterContext.ActionDescriptor.GetCustomAttributes(typeof(AnonymousAttribute), true);
//是否是Anonymous
var Anonymous = attrsAnonymous.Length == ;
return Anonymous;
}
/// <summary>
/// [LoginAllowView标记]验证是否登录就可以访问(如果已经登陆,那么不对于标识了LoginAllowView的方法就不需要验证了)
/// </summary>
/// <param name="filterContext"></param>
/// <returns></returns>
public bool CheckLoginAllowView(ActionExecutingContext filterContext)
{
//在这里允许一种情况,如果已经登陆,那么不对于标识了LoginAllowView的方法就不需要验证了
object[] attrs = filterContext.ActionDescriptor.GetCustomAttributes(typeof(LoginAllowViewAttribute), true);
//是否是LoginAllowView
var ViewMethod = attrs.Length == ;
return ViewMethod;
} /// <summary>
/// //权限判断业务逻辑
/// </summary>
/// <param name="filterContext"></param>
/// <param name="isViewPage">是否是页面</param>
/// <returns></returns>
protected virtual bool AuthorizeCore(ActionExecutingContext filterContext)
{ if (filterContext.HttpContext == null)
{
throw new ArgumentNullException("httpContext");
}
//验证当前Action是否是匿名访问Action
if (CheckAnonymous(filterContext))
return true;
//未登录验证
if (SessionHelper.Get("UserID") == null)
{
return false;
}
//验证当前Action是否是登录就可以访问的Action
if (CheckLoginAllowView(filterContext))
return true; //下面开始用户权限验证
var user = new UserService();
SysCurrentUser CurrentUser = new SysCurrentUser();
var controllerName = filterContext.RouteData.Values["controller"].ToString();
var actionName = filterContext.RouteData.Values["action"].ToString();
//如果是超级管理员,直接允许
if (CurrentUser.UserID == ConfigSettings.GetAdminUserID())
{
return true;
}
//如果拥有超级管理员的角色就默认全部允许
string AdminUserRoleID = ConfigSettings.GetAdminUserRoleID().ToString();
//检查当前角色组有没有超级角色
if (Tools.CheckStringHasValue(CurrentUser.UserRoles, ',', AdminUserRoleID))
{
return true;
} //Action权限验证
if (controllerName.ToLower() != "manage")//如果当前Action请求为具体的功能页并且不是Manage中 Index页和Welcome页
{
//验证
if (!user.RoleHasOperatePermission(CurrentUser.UserRoles, controllerName, actionName))//如果验证该操作是否拥有权限
{
return false;
}
}
//管理页面直接允许
return true;
}
}
}
ASP.NET MVC4 权限验证的更多相关文章
- ASP.NET MVC权限验证 封装类
写该权限类主要目地 为了让权限配置更加的灵活,可以根据SQL.json.或者XML的方式来动态进行页面的访问控制,以及没有权限的相关跳转. 使用步骤 1.要建一个全局过滤器 //受权过滤器 publi ...
- ASP.NET通用权限验证组件实现
沙发(SF)通用权限验证组件 开篇 本篇介绍通用权限验证的实现代码思路,总共分为导入参数.解析XML.根据XML配置进行处理.返回结果. 代码架构图 1. 类介绍 1.SFWebPermissio ...
- 【ASP.NET】ASP.NET中权限验证使用OnAuthorization实现
在项目开发中,通常我们都会涉及到用户登录才能访问的网页,比如购物网站,我们浏览商品,添加购物车(以前开发的时候在这里就需要登录用户,但是现在有了缓存的实现,这里可以将商品加入缓存,等到结账的时候再登录 ...
- ASP.NET MVC4系列验证机制、伙伴类共享源数据信息(数据注解和验证)
一,mvc前后台验证 自定义属性标签MyRegularExpression using System; using System.Collections.Generic; using System.C ...
- asp.net mvc4 远程验证
[HttpGet] public ActionResult CheckToolsIdExists(string ToolsID) { using (BaseContext context = new ...
- Asp.net MVC 权限验证,以及是否允许匿名访问
public class CheckUserAttribute : ActionFilterAttribute, IAuthorizationFilter { public void OnAuthor ...
- ASP.NET通用权限组件思路设计
开篇 做任何系统都离不开和绕不过权限的控制,尤其是B/S系统工作原理的特殊性使得权限控制起来更为繁琐,所以就在想是否可以利用IIS的工作原理,在IIS处理客户端请求的某个入口或出口通过判断URL来达到 ...
- NET MVC权限验证
ASP.NET MVC权限验证 封装类 写该权限类主要目地 为了让权限配置更加的灵活,可以根据SQL.json.或者XML的方式来动态进行页面的访问控制,以及没有权限的相关跳转. 使用步骤 1.要建一 ...
- 从零开始实现asp.net MVC4框架网站的用户登录以及权限验证模块 详细教程
从零开始实现asp.net MVC4框架网站的用户登录以及权限验证模块 详细教程 用户登录与权限验证是网站不可缺少的一部分功能,asp.net MVC4框架内置了用于实现该功能的类库,只需要简单搭 ...
随机推荐
- java使用链栈实现迷宫求解
java实现链栈在前面有所介绍:http://www.cnblogs.com/lixiaolun/p/4644141.html java实现链栈的代码: package stackapplicatio ...
- java 和 C# 响应输出的相似度
java servlet response: bf.append("Shipment No, STT No, WIN Event, DateTime, WOU Envent, DateTim ...
- Java之字节码(3) - 简单介绍
转载来自 首先了解一下理论知识: 字节码: Class文件是8位字节流,按字节对齐.之所以称为字节码,是因为每条指令都只占据一个字节,所有的操作码和操作数都是按字节对齐的.如:0×03表示iconst ...
- xml xpath dta笔记
xml: 有且只有一个根元素 默认utf-8 如果是中文且为不是utf-8的必须指定编码 声明的编码必须和文档的内容保持一致 well-formed XML :是否符合xml语法 valid xml: ...
- 【Redis】redis 五种数据结构详解(string,list,set,zset,hash)
redis 五种数据结构详解(string,list,set,zset,hash) Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存 ...
- SyntaxError: Non-ASCII character '\xe5' in file index.py on line 6, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
python入门,hhh 在慕课网上学习python入门,编写汉诺塔的递归调用时,代码正确.但是加上注释后编译不通过 报如下错误: SyntaxError: Non-ASCII character , ...
- SQLyog之MySQL客户端的下载、安装和使用(旗舰版)(推荐)
不多说,直接上干货! 福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 ...
- Json映射为Map,避免定义过多pojo类
我们在开发过程中经常会遇到解析Json字符串的情况,这时候采用开源工具可以快速将json字符串映射为pojo对象.而在某些业务场景中,往往为了获得json字符串中某个属性的值而不得不定义一个pojo类 ...
- [Jobdu] 题目1139:最大子矩阵
题目描述: 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵.比如,如下4 * 4的矩阵 0 -2 -7 09 2 -6 2-4 1 -4 ...
- wpa_cli 连接 wifi(转)
wpa_cli 连接 wifi 转自:http://hi.baidu.com/yyangjjun/item/9dfe8e175439fc7a1009b5ba 1: run wpa_supplica ...