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框架内置了用于实现该功能的类库,只需要简单搭 ...
随机推荐
- C#应用视频教程3.3 Halcon+C#测试
接下来我们考虑把Halcon的代码移植到C#程序上,首先找到halcon的dll(.NET类库有1.0,2.0,3.5的,如果你安装了更新版本的halcon则有更新的.NET类库,我们复制最新的dll ...
- vue常用属性解释。
props:详看 示例-网格组件. props 可以是数组或对象,用于接收来自父组件的数据.props 可以是简单的数组,或者使用对象作为替代,对象允许配置高级选项,如类型检测.自定义校验和设置默认值 ...
- 算法笔记_144:有向图强连通分量的Tarjan算法(Java)
目录 1 问题描述 2 解决方案 1 问题描述 引用自百度百科: 如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连 ...
- Java Tread多线程(1)实现Runnable接口
作者 : 卿笃军 原文地址:http://blog.csdn.net/qingdujun/article/details/39347245 本文演示,Tread多线程实现Runnable接口,以及简单 ...
- 【TP5.0】页面布局,引入公共的模版文件
1.实例:如后台admin模块,公用一个header.html和footer.hml 2.模块结构: 3.使用方式: {include file="common/header"} ...
- 利用Oracle 发送邮件(utl_smtp)
发送邮件的方法有很多,.NET前台也可以通过创建邮件类的形式, 通过微软提供的System.Net.Mail.dll 也可以简单的发送邮件.但是代码比较长,操作起来虽然很简单(很多细节忽略了). 这里 ...
- MySQL 添加外键约束,不检查现有数据
这可能是MySQL在InnoDB中设置了foreign key关联,造成无法更新或删除数据.可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况. SET FOREIGN_KEY_CHE ...
- Android实现开机自动运行程序
有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service.怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以“Don't call me, I'll call y ...
- C#:Ini文件操作(待补充)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
- IIS目录禁止执行权限
IIS6: IIS7: