MVC中如何利用filter实现权限管理
2010-08-11 11:58
459 查看
定义filter:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using System.Web.Routing;
using Vslea.Models;
namespace Vslea.Filters
{
public class RoleAttribute:ActionFilterAttribute,IAuthorizationFilter
{
public RoleAttribute(RoleTypeEnum roleType)
{
this.RoleType = roleType;
}
public RoleTypeEnum RoleType { get; set; }
public void OnAuthorization(AuthorizationContext filterContext)
{
HttpContextBase b = filterContext.RequestContext.HttpContext;
UserModel model = b.Session["User"] as UserModel;
if (model != null)
{
if (model.RoleType != this.RoleType)
{
filterContext.Result = new RedirectToRouteResult("default", new RouteValueDictionary(new { controller = "Shared", action = "Error" }));
}
}
}
}
}
在controller中:
[AcceptVerbs(HttpVerbs.Get)]
[Role(RoleTypeEnum.Admin)]
public ActionResult NewCustomer(int? customerId)
{
}
简单又方便,使代码结构更加清晰
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using System.Web.Routing;
using Vslea.Models;
namespace Vslea.Filters
{
public class RoleAttribute:ActionFilterAttribute,IAuthorizationFilter
{
public RoleAttribute(RoleTypeEnum roleType)
{
this.RoleType = roleType;
}
public RoleTypeEnum RoleType { get; set; }
public void OnAuthorization(AuthorizationContext filterContext)
{
HttpContextBase b = filterContext.RequestContext.HttpContext;
UserModel model = b.Session["User"] as UserModel;
if (model != null)
{
if (model.RoleType != this.RoleType)
{
filterContext.Result = new RedirectToRouteResult("default", new RouteValueDictionary(new { controller = "Shared", action = "Error" }));
}
}
}
}
}
在controller中:
[AcceptVerbs(HttpVerbs.Get)]
[Role(RoleTypeEnum.Admin)]
public ActionResult NewCustomer(int? customerId)
{
}
简单又方便,使代码结构更加清晰
相关文章推荐
- 利用mvc filterconfig属性实现权限验证
- asp.net mvc 5 利用ActionFilterAttribute实现权限过滤
- 如何利用简道云实现专柜管理? | 数据管理
- springmvc+shiro+maven 实现登录认证与权限授权管理
- 如何用vsftpd实现用户不同权限:只能下载,可上传,管理权限等
- 利用ZTree链接数据库实现 [权限管理]
- ASP.NET MVC 中如何实现基于角色的权限控制
- ★★★★★手把手教你如何利用凤凰实现破 解后台权限以及升级固件(刷机)★★★★★
- 利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载
- asp.net mvc +easyui 实现权限管理(二)
- 回答客户对通用权限的疑问,页面里如何判断权限?如何实现上级能管理下级?基础表用哪个?
- java annotation + springMVC 实现用户角色权限管理
- ASP.NET MVC 中如何实现基于角色的权限控制
- ASP.NET MVC 中如何实现基于角色的权限控制
- ASP.NET MVC 利用ActionFilterAttribute来做权限等
- 回答客户对通用权限的疑问,页面里如何判断权限?如何实现上级能管理下级?基础表用哪个?
- PHP巧妙利用位运算实现网站权限管理
- [MySQL FAQ]系列 -- 如何利用触发器实现账户权限审计
- 利用ZTree链接数据库实现 [权限管理]
- MVC 自定义AuthorizeAttribute实现权限管理