关于AuthorizeAttribute使用
在开发中,假如你只对一个角色进行权限处理,你可以这么写
class ActionAuthAttribute : AuthorizeAttribute
{
private RoleType _roleType;
public ActionAuthAttribute(RoleType role)
{
_roleType = role;
} protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (BaseController.CurrentUser.RoleId == (int)_roleType )
{
return true;
}
else
{
return false;
}
} protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
//base.HandleUnauthorizedRequest(filterContext);
//filterContext.HttpContext.Response.RedirectToRoute("ErrorPage", new { msg = HttpUtility.UrlEncodeUnicode("你无权访问此页面!") });
System.Web.HttpContext.Current.Response.RedirectToRoute("ErrorPage", new { msg = HttpUtility.UrlEncodeUnicode("你无权访问此页面!") }); }
}
但是当两个角色都有权限呢?
方法一:你可以重写构造函数,如下
class ActionAuthAttribute : AuthorizeAttribute
{
private RoleType _roleType;
private RoleType _roleType1;
private RoleType _roleType2;
public ActionAuthAttribute(RoleType role)
{
_roleType = role;
}
public ActionAuthAttribute(RoleType role1, RoleType role2)
{
_roleType1 = role1;
_roleType2 = role2;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (BaseController.CurrentUser.RoleId == (int)_roleType )
{
return true;
}
else if (BaseController.CurrentUser.RoleId == (int)_roleType1 || BaseController.CurrentUser.RoleId == (int)_roleType2)
{
return true;
}
else
{
return false;
}
} protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
//base.HandleUnauthorizedRequest(filterContext);
//filterContext.HttpContext.Response.RedirectToRoute("ErrorPage", new { msg = HttpUtility.UrlEncodeUnicode("你无权访问此页面!") });
System.Web.HttpContext.Current.Response.RedirectToRoute("ErrorPage", new { msg = HttpUtility.UrlEncodeUnicode("你无权访问此页面!") }); }
}
方法二:你可以使用
params定义一个变化的数组参数,这样参数多少就可以随你了,推荐第二种方法,不然,随着参数变化,你要一直重写函数了。。哈哈
[AttributeUsage(AttributeTargets.Method)]
class ActionAuthAttribute : AuthorizeAttribute
{
private RoleType[] _roleType;
public ActionAuthAttribute(params RoleType[] role)
{
_roleType = role;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
foreach (var item in _roleType)
{
if (BaseController.CurrentUser.RoleId == (int)item)
{
return true;
}
}
return false;
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
var routeValue = new RouteValueDictionary {
{ "Controller", "Etc"},
{ "Action", "Oops"},
{"msg", HttpUtility.UrlEncodeUnicode("你无权访问此页面!")}
}; filterContext.Result = new RedirectToRouteResult(routeValue);
}
关于AuthorizeAttribute使用的更多相关文章
- ASP.NET MVC中利用AuthorizeAttribute实现访问身份是否合法以及Cookie过期问题的处理
话说来到上海已经快半年了,时光如白驹过隙,稍微不注意,时间就溜走了,倒是没有那么忙碌,闲暇之际来博客园还是比较多的,记得上次在逛博问的时候看到有同志在问MVC中Cookie过期后如何作相关处理,他在阐 ...
- [转]How do you create a custom AuthorizeAttribute in ASP.NET Core?
问: I'm trying to make a custom authorization attribute in ASP.NET Core. In previous versions it was ...
- ASP.MVC 基于AuthorizeAttribute权限设计案例
ASP.MVC上实现权限控制的方法很多,比如使用AuthorizeAttribute这个特性 1.创建自定义特性用于权限验证 public class AuthorizeDiy : Authorize ...
- 【记录】ASP.NET MVC AuthorizeAttribute OnAuthorization 验证跳转
重写 AuthorizeAttribute 的 OnAuthorization 方法: using System.Web.Mvc; namespace Demo.Web.Common { public ...
- 扩展AuthorizeAttribute
MVC中经常会用到关于设置访问权限的问题: 如果我们扩展了AuthorizeAttribute,那么我们只需要在类或方法前加上此attribute,即可实现权限问题. AttributeTargets ...
- 爱上MVC~AuthorizeAttribute验证不通过如何停止当前上下文
回到目录 我们知道mvc里有一些过滤器,AuthorizeAttribute用来做授权,一般在用户授权方面可以使用它,当使用没有登陆,我们直接跳到登陆页,这是没有问题的,可我要说的是,当用户对某个Ac ...
- MVC5的AuthorizeAttribute详解
现今大多数的网站尤其是购物网站都要求你登录后才能继续操作,当你匿名的将商品放入购物车后,不可能匿名购买这时可以转到登录界面让用户进行登录验证. 适用系统自带的过滤器 MVC5只要将属性[Authori ...
- ASP.NET MVC自定义AuthorizeAttribute篇知识点讲解—登录限制
1.前言 a.微软对ASP.NET的开发从WebForm到MVC的转变,已经正式过去5,6个年头,现在WebForm和MVC也都越来越完善,小小算来我也已经工作了将近三年,从大学的时候学习ASP.NE ...
- 利用AuthorizeAttribute属性简单避免 MVC 中的跨域攻击
跨域攻击---自然来路页面和目标页面不在同一个域下,所以直接判断来路域和当前自己的域就可以了. 可以广泛应用于表单提交,ajax调用或者某些不想让用户直接输入网址看到的页面 [csharp] view ...
- MVC中使用AuthorizeAttribute做身份验证操作
代码顺序为:OnAuthorization-->AuthorizeCore-->HandleUnauthorizedRequest 如果AuthorizeCore返回false时,才会走H ...
随机推荐
- 【转】Spring注解详解
http://blog.csdn.net/xyh820/article/details/7303330/ 概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类 ...
- 一份React-Native学习指南-感谢分享
自己在学习React-Native过程中整理的一份学习指南,包含 教程.开源app和资源网站等,还在不断更新中.欢迎pull requests! React-Native学习指南 本指南汇集React ...
- JS获取select选中的值
var oSel=oFl.getElementsByTagName('select')[0]; oSel.onchange=function(){ var indexselect=oSel.selec ...
- Phalcon的学习篇-phalcon和devtools的安装和设置
A Phalcon在Windows上的安装 1 从Phalcon for Windows下载适合的DLL, 这里的适合 主要看两个方面 1 PHP的版本 2 线程是否是安全 3 编译版本 如果不清楚这 ...
- unity 基础学习 transform
unity 基础学习 transform 1.unity采用的是右手坐标系,X轴右手为+,Y轴向上为+,Z轴朝里为+; 但是我们从3D MAX中导入模型之后,发现轴向并没有遵从这个原理, 其实是 ...
- 深入浅出 Java Concurrency (4): 原子操作 part 3 指令重排序与happens-before法则
转: http://www.blogjava.net/xylz/archive/2010/07/03/325168.html 在这个小结里面重点讨论原子操作的原理和设计思想. 由于在下一个章节中会谈到 ...
- COJ 2004 序列
传送门:http://oj.cnuschool.org.cn/oj/home/addSolution.htm?problemID=978 试题描述: WZJ的数字游戏又开始了.他写了N个自然数Ai到黑 ...
- 【动态规划】Vijos P1143 三取方格数(NOIP2000提高组)
题目链接: https://vijos.org/p/1143 题目大意: NxN的矩阵,每个值只能取一次,从(1,1)走到(n,n)走三次能取得的最大值. 题目思路: [动态规划] f[x1][y1] ...
- 连接Oracle的几种方式
如何引用Data.OracleClient.dll 由于从.net 4.0之后,微软将OracleClient.dll从框架里去除了,所以要使用,需要在VS2010里面去把项目的.net框架从.net ...
- C++程序原码
直接插入排序基本算法 #include<iostream.h> #include<stdlib.h> #include<time.h> const int n=10 ...