关于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 ...
随机推荐
- WAMP集成环境更改web根目录
使用WAMP集成环境,如何更改web根目录 做php开发使用WAMP集成环境的同学大部分有过这样的经历:如果你试图修改web根目录,那么你肯定会想到要修改apache/apache2.2.11/con ...
- 解读为什么有符号的char可表示范围是-128~+127
问:为什么有符号的char可表示范围是-128~+127? 要明白这个问题,首先要明白一下几点: 对于char和int计算机中以补码形式存在. 严格来说计算机就是傻逼,它只知道某个位上是0还是1. 我 ...
- SVN版本回滚~
如果你在svn上对文件进行编辑作了修改,想撤销,那么有两种方法可以还原:1) svn revert <yourfile>2) 手动删除该文件,重新执行svn up(rm <yourf ...
- mysql的SQL_CALC_FOUND_ROWS 使用
mysql的SQL_CALC_FOUND_ROWS 使用 标签: sqlmysqltable 2007-02-27 11:40 5073人阅读 评论(0) 收藏 举报 分类: Mysql数据库技术( ...
- "sessionFactory " or "hibernateTemplate " is required异常
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http:/ ...
- Unity3d开发IOS游戏 基础
Unity3d开发IOS游戏 基础 @阿龙 - 649998群 1.先说明两个问题,我在WIN7下面的U3D里面,用了雅黑字体,但是导出为ios后,字体就看不见了,这是为什么呢?这是需要在MAC下找 ...
- Little Kings
sgu223:http://acm.sgu.ru/problem.php?contest=0&problem=223 题意:n*n的格子放k个国王,一共有多少种放发. 题解:简单的状压DP. ...
- Area
http://poj.org/problem?id=1265 #include<cstdio> #include<istream> #include<algorithm& ...
- keil uVision4一些使用总结(汉字注释,C关键字等)
近日心血来潮,下载了最新的版的keil,再加上protues ,想弄个虚拟环境.主要原因还是经济问题.电子元件,是要花钱的... 今天遇到些keil uVision 4使用方面的问题,记录下来,方便以 ...
- Linux中查看进程的多线程pstree, ps -L
Linux中查看进程的多线程 在SMP系统中,我们的应用程序经常使用多线程的技术,那么在Linux中如何查看某个进程的多个线程呢? 本文介绍3种命令来查看Linux系统中的线程(LWP)的情况:在我的 ...