在开发中,假如你只对一个角色进行权限处理,你可以这么写

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使用的更多相关文章

  1. ASP.NET MVC中利用AuthorizeAttribute实现访问身份是否合法以及Cookie过期问题的处理

    话说来到上海已经快半年了,时光如白驹过隙,稍微不注意,时间就溜走了,倒是没有那么忙碌,闲暇之际来博客园还是比较多的,记得上次在逛博问的时候看到有同志在问MVC中Cookie过期后如何作相关处理,他在阐 ...

  2. [转]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 ...

  3. ASP.MVC 基于AuthorizeAttribute权限设计案例

    ASP.MVC上实现权限控制的方法很多,比如使用AuthorizeAttribute这个特性 1.创建自定义特性用于权限验证 public class AuthorizeDiy : Authorize ...

  4. 【记录】ASP.NET MVC AuthorizeAttribute OnAuthorization 验证跳转

    重写 AuthorizeAttribute 的 OnAuthorization 方法: using System.Web.Mvc; namespace Demo.Web.Common { public ...

  5. 扩展AuthorizeAttribute

    MVC中经常会用到关于设置访问权限的问题: 如果我们扩展了AuthorizeAttribute,那么我们只需要在类或方法前加上此attribute,即可实现权限问题. AttributeTargets ...

  6. 爱上MVC~AuthorizeAttribute验证不通过如何停止当前上下文

    回到目录 我们知道mvc里有一些过滤器,AuthorizeAttribute用来做授权,一般在用户授权方面可以使用它,当使用没有登陆,我们直接跳到登陆页,这是没有问题的,可我要说的是,当用户对某个Ac ...

  7. MVC5的AuthorizeAttribute详解

    现今大多数的网站尤其是购物网站都要求你登录后才能继续操作,当你匿名的将商品放入购物车后,不可能匿名购买这时可以转到登录界面让用户进行登录验证. 适用系统自带的过滤器 MVC5只要将属性[Authori ...

  8. ASP.NET MVC自定义AuthorizeAttribute篇知识点讲解—登录限制

    1.前言 a.微软对ASP.NET的开发从WebForm到MVC的转变,已经正式过去5,6个年头,现在WebForm和MVC也都越来越完善,小小算来我也已经工作了将近三年,从大学的时候学习ASP.NE ...

  9. 利用AuthorizeAttribute属性简单避免 MVC 中的跨域攻击

    跨域攻击---自然来路页面和目标页面不在同一个域下,所以直接判断来路域和当前自己的域就可以了. 可以广泛应用于表单提交,ajax调用或者某些不想让用户直接输入网址看到的页面 [csharp] view ...

  10. MVC中使用AuthorizeAttribute做身份验证操作

    代码顺序为:OnAuthorization-->AuthorizeCore-->HandleUnauthorizedRequest 如果AuthorizeCore返回false时,才会走H ...

随机推荐

  1. IOS--UIImageView的使用方法

    IOS--UIImageView的使用方法 //初始化 UIImageView  *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(10 ...

  2. python Hbase Thrift pycharm 及引入包

    cp -r hbase/ /usr/lib/python2.7/site-packages/ 官方示例子http://code.google.com/p/hbase-thrift/source/bro ...

  3. [BZOJ 3172] [Tjoi2013] 单词 【AC自动机】

    题目链接:BZOJ - 3172 题目分析: 题目要求求出每个单词出现的次数,如果把每个单词都在AC自动机里直接跑一遍,复杂度会很高. 这里使用AC自动机的“副产品”——Fail树,Fail树的一个性 ...

  4. nosql db and javascript performance

    http://blog.csdn.net/yiqijinbu/article/details/9053467 http://blog.nosqlfan.com/tags/javascript http ...

  5. Agri-Net

    poj1258:http://poj.org/problem?id=1258 题意:生成树的模板题.简单题. #include<iostream> #include<cstring& ...

  6. Hibernate 的*.hbm.xml文件的填写技巧

    ================================================================================= 模板: <!-- ?属性,本类 ...

  7. 带你走进EJB--MDB实现发送邮件(1)

    在实际的项目中我们有这样的需求,用户注册网站成功之后系统会自动的给注册用户发送注册成功通知邮件,而发送通知邮件的具体过程我们可以通过MDB来实现. 在用MDB来实现发送通知过程之前我们需要先了解一下J ...

  8. Linux下Socket编程的端口问题( Bind error: Address already in use )

    Linux下Socket编程的端口问题( Bind error: Address already in use ) 在进行linux网络编程时,每次修改了源代码并再次编译运行时,常遇到下面的地使用错误 ...

  9. C++中自己理解的一些细节哈

    对于大牛来说,我写的可能很低级哈,留给自己看,以后遇到的都慢慢补充哈!^^ 1.每一个类定义结束后,不要忘了在类的结束符"}"后面加上一个";"哦! 2.构造函 ...

  10. Delphi NativeXML 乱码的问题

    我遇到 NativeXML 在它的一个节点的属性上面写入属性,但是当读出的值中包含汉字的时候出现了乱码.检查代码如下 NativeXml := TNativeXml.Create; try Nativ ...