关于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 ...
随机推荐
- Apache:To Config The Vhost of Django Project
It is not a good idea to use dev server in Production Environment. Apache or Nginx are good choice.B ...
- UIView设置背景渐变色
UIView设置背景渐变色 // Allocate bitmap context CGContextRef bitmapContext = CGBitmapContextCreate(NULL, , ...
- Egret 纹理、计时器
1. 九宫切 典型例子就是圆角矩形的拉伸问题. 先去P一张绿色的圆角矩形. private createGameScene():void { var box:egret.Bitmap = new eg ...
- 【技术贴】7-zip 7z关联右键菜单后右键不弹出菜单的解决办法
解决7z,7zip右键菜单 失效 右键菜单 无法弹出右键菜单 不正常 右键菜单 sb等各种疑难杂症. 1.首先先去7z的选项里面把右键关联给设置了. 用的好好的7z,正吃着火锅唱着歌忽然发现右键单文件 ...
- About JavaScript
JavaScript Function, Constructor function, Plain Object (expression function, closure) Maintainable ...
- 揪出“凶手”——实战WinDbg分析电脑蓝屏原因
http://www.appinn.com/blue-screen-search-code/ 蓝屏代码查询器 – 找出蓝屏的元凶 11 文章标签: windows / 系统 / 蓝屏. 蓝屏代码查询器 ...
- github Travis CI 持续集成
一个项目如何保证代码质量是开发中非常重要的环节,对于开源项目来说更是如此,因为开源项目要面对的是来自不同水平开发者提交的代码.所以围绕开源做持续集成(Continuous Integration)变得 ...
- android 随手记 自定义广播通知栏
自定义通知栏图标?不是很简单么.自定义布局都不在话下! 是的,有xml布局文件当然一切都很简单,如果不给你布局文件用呢? 听我慢慢道来! 首先怎么创建一个通知呢? 1.new 一个 Notificat ...
- IMAP与POP3的区别
POP3协议允许电子邮件客户端下载服务器上的邮件,但是在客户端的操作(如移动邮件.标记已读等),不会反馈到服务器上.比如通过客户端收取了邮箱中的2封邮件并移动到其他文件夹,邮箱服务器上的这些邮件是没有 ...
- vim学习与理解
1. 转变思维 --> vim 无鼠标文本编辑工具 在鸟哥的linux私房菜中,是这么说明linux的: 1. vim是linux like系统中非常强大的一个文本编辑工具,历史悠久,很多系统都 ...