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

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. 快速访问WCF服务--ServiceModel 元数据实用工具 (Svcutil.exe)

    基本定义 ServiceModel 元数据实用工具用于依据元数据文档生成服务模型代码,以及依据服务模型代码生成元数据文档. SvcUtil.exe ServiceModel 元数据实用工具可在 Win ...

  2. Laravel框架——自己写的类找不到

    composer.json my model files are stored in directory of app\models, therefor "autoload": { ...

  3. wildcard 处理全部文件

    Makefile如果想取得文件夹下全部文件 $(wildcard $(PATH)/*.c) 即可

  4. GNU PID

    多进程编程 写在前面的话 本文主要根据本人在UNIX系统上的编程实践经验总结而成, 既做为自己在 一个时期内编程实践的部分总结, 又可成为文章发表. 对UNIX程序员初学者来 说是一个小小的经验, 仅 ...

  5. Google研究员Ilya Sutskever:成功训练LDNN的13点建议

    Google研究员Ilya Sutskever:成功训练LDNN的13点建议 摘要:本文由Ilya Sutskever(Google研究员.深度学习泰斗Geoffrey Hinton的学生.DNNre ...

  6. Eclipse中查找接口实现类快捷键

    就是点击某个接口某个方法名字的时候,直接跳到它的某个实现类里面,一般我们习惯对着那个接口的方法按F3,但是这会直接跳到接口类的源码中,那么呵呵,我们换一个ctrl+T 然后自己选择一下实现类就进去了. ...

  7. Biathlon Track

    Codeforces Round #242 (Div. 2) D:http://codeforces.com/contest/424/problem/D 题意:给你一个n*m的矩阵,每个格子上面有个数 ...

  8. MySql密码丢失

     windows下mysql密码忘记了 第一步:netstat -nat(可以查看mysql是否启动了,如果启动了,可以用输入net stop mysql(或者通过任务管理器结束进程)) 第二步:my ...

  9. C Looooops(扩展欧几里得求模线性方程)

    http://poj.org/problem?id=2115 题意:对于C的循环(for i = A; i != B; i+=C)问在k位存储系统内循环多少次结束: 若循环有限次能结束输出次数,否则输 ...

  10. Linux Shell编程(3)——运行shell脚本

    写完一个脚本,你能够运行它用命令:sh scriptname, [5] 另外也也可以用bash scriptname. 来执行(不推荐使用:sh <scriptname, 因为这样会禁止脚本从标 ...