ASP.NET MVC使用Filter实现页面验证与授权

关于ASP.NET MVC的Filter(过滤器的讲解)见:http://www.cnblogs.com/BreatheLee/archive/2012/06/07/2540469.html

关于授权的实现:http://www.cxy.me/doc/4660.htm

 

在ASP.NET MVC中,如何来实现表单的验证与授权访问呢?至少在CTP3中,还没有一个官方的解决方案。ASP.NET WebForm的表单验证和授权机制是否适合在ASP.NET MVC中使用呢?带着这些问题来进入我们今天的主题。

在ASP.NET WebForm的架构下,我们可以通过一定的配置即可实现用户身份验证和授权。特别是在ASP.NET 2.0的Membership功能的支撑下,可以做到更加简洁可复用的用户验证系统。通过web.config可以做到对页面或目录对不同用户身份可见性的定制,但是它是基于物理文件和目录。而在ASP.NET MVC架构下,用户访问的每一个页面在磁盘中并没有一个固定的物理文件,它是通过Controller控制数据与视图的组合来生成HTML代码,进而向客户端输出。那么我们该如何来复用已有的表单验证授权机制呢?

在MVC中,请求的功能入口是Controller相应的Action函数,我们可以在函数执行前去控制请求权限。在ASP.NET MVC Preview 2后,提供了一个机制让我们可以对Action的AOP拦截,这个接口定义如下:

public interface IActionFilter 
{
 void OnActionExecuting(ActionExecutingContext filterContext); 
 void OnActionExecuted(ActionExecutedContext filterContext); 
}


我们有两种方式来实现拦截,一种我们可以通过定义Attribute来实现拦截的功能,在System.Web.Mvc程序集中有一个ActionFilterAttribute抽象类,通过重写这个抽象类的这些虚方法,我们就可以实现对特定的执行过程进行拦截。

另一种方法,我们注意到Controller这个类也实现了IActionFilter这个接口,并且也提供了这四个函数的虚拟方法定义。框架内部,在调用Action方法的时候同时来调用这些拦截方法。具体的可以参考:ControllerActionInvoker 这个类的实现,所有的Action的调用都在这个类当中被实现。所以我们只要重写Controller里这四个虚方法,也可完成本Controller面的所有Action的拦截。

在这里,我也找到了国外友人已经实现好的基于角色的MVC权限控制的方案。自定义了两个自定义Attribute,分别为:RequiresAuthenticationAttribute和RequiresRoleAttribute。通过这两个Attribute来可以作用于Class和Method,用标记哪些Controller或Action需要登录后,或者需要拥有哪些角色才能执行。如果用户没有拥有访问当然Controller或Action权限的时候,就会自动被重定向到登录页面去。下面是两个类的定义:

/// <summary> 
/// Checks the User's authentication using FormsAuthentication 
/// and redirects to the Login Url for the application on fail 
/// </summary> 
[RequiresAuthentication] 
public class RequiresAuthenticationAttribute : ActionFilterAttribute 
{ 

public override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 

//redirect if not authenticated 
if (!filterContext.HttpContext.User.Identity.IsAuthenticated) 
{ 

//use the current url for the redirect 
string redirectOnSuccess = filterContext.HttpContext.Request.Url.AbsolutePath; 

//send them off to the login page 
string redirectUrl = string.Format("?ReturnUrl={0}", redirectOnSuccess); 
string loginUrl = FormsAuthentication.LoginUrl + redirectUrl; 
filterContext.HttpContext.Response.Redirect(loginUrl, true); 

} 
} 
} 

/// <summary> 
/// Checks the User's role using FormsAuthentication 
/// and throws and UnauthorizedAccessException if not authorized 
/// </summary> 
public class RequiresRoleAttribute : ActionFilterAttribute 
{ 

public string RoleToCheckFor { get; set; } 

public override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 
//redirect if the user is not authenticated 
if (!String.IsNullOrEmpty(RoleToCheckFor)) 
{ 

if (!filterContext.HttpContext.User.Identity.IsAuthenticated) 
{ 

//use the current url for the redirect 
string redirectOnSuccess = filterContext.HttpContext.Request.Url.AbsolutePath; 

//send them off to the login page 
string redirectUrl = string.Format("?ReturnUrl={0}", redirectOnSuccess); 
string loginUrl = FormsAuthentication.LoginUrl + redirectUrl; 
filterContext.HttpContext.Response.Redirect(loginUrl, true); 

} 
else 
{ 
bool isAuthorized = filterContext.HttpContext.User.IsInRole(this.RoleToCheckFor); 
if (!isAuthorized) 
throw new UnauthorizedAccessException("You are not authorized to view this page"); 
} 
} 
else 
{ 
throw new InvalidOperationException("No Role Specified"); 
} 
} 
} 

如上所介绍的两种方法,我们一样可以定义一个Controller基类,通过拦截来进行权限的控制。但是与定义Attribute相比,手法并不是很好,也不利于通用化。但是就理论上的性能来说,会比Attribute更好。

到目前为止,ASP.NET MVC还没有更新的消息,我想在正式版本的ASP.NET MVC框架,权限控制问题会有一个官方说法。希望到时候会有一种更为灵活和可配置的方案。也许通过控制Url来控制访问权限也是一种可行的方案,会不会集成到RouteTable里面呢?让我们试目以待吧。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值