MVC使用Controller代替Filter完成登录验证(Session校验)学习笔记5

所属分类: 网络编程 / ASP.NET 阅读数: 1289
收藏 0 赞 0 分享

之前的学习中,在对Session校验完成登录验证时,通常使用Filter来处理,方法类似与前文的错误日志过滤,即新建Filter类继承ActionFilterAttribute类,重写OnActionExecuting方法,之后直接在需要验证的Action前加上Filter标记即可。

1. 新建登陆校验类CheckLoginAttribute

using System.Web.Mvc;

namespace PMS.WebApp.Models
{
  public class CheckLoginAttribute:ActionFilterAttribute
  {
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
      base.OnActionExecuting(filterContext);
      if (filterContext.HttpContext.Session == null || filterContext.HttpContext.Session["user"] == null)
      {
        filterContext.HttpContext.Response.Redirect("/User/Login");
      }
    }
  }
}

2. 在需要校验的Action增加标记以完成校验

using System.Web.Mvc;
using PMS.IBLL;
using PMS.WebApp.Models;

namespace PMS.WebApp.Controllers
{
  public class UserController : Controller
  {
    //
    // GET: /User/
    //private IUserService _userService;
    //private IUserService UserService
    //{
    //  get { return _userService ?? (_userService = new UserService()); }
    //  set { _userService = value; }
    //}
    private IUserService UserService { get; set; }
    [CheckLogin]
    public ActionResult Index()
    {
      return Content("OK");
    }

  }
}

注意:不要在RegisterGlobalFilters方法中注册校验类,否则则会相当于给所有Action都添加了校验

这种方法使用起来需要在每个Action方法前添加过滤标签,且效率并不十分高,我们的项目中使用的是一种更为简单高效的方法:使用Controller进行登录验证

1.  新建一个用于验证的Controller父类,并在其内重写OnActionExecuting方法完成登陆校验:

using System.Web.Mvc;

namespace PMS.WebApp.Controllers
{
  public class FilterController : Controller
  {
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
      base.OnActionExecuting(filterContext);
      if (Session["user"] == null)
      {
        //filterContext.HttpContext.Response.Redirect("/User/Login");
        filterContext.Result = Redirect("/User/Login");
      }
    }
  }
}

在Controller校验类的OnActionExecuting方法中,有如下代码

//filterContext.HttpContext.Response.Redirect("/User/Login");
filterContext.Result = Redirect("/User/Login");      

我们使用后者而放弃前者的原因是,ASP.NET MVC中规定,Action必须返回ActionResult,如果使用前者,在完成跳转前会先进入到请求的页面,这样不符合我们使用过滤器的初衷。

2.  然后使需要校验的Controller继承于我们定义的校验Controller即可完成全局登录校验操作:

using System.Web.Mvc;
using PMS.IBLL;

namespace PMS.WebApp.Controllers
{
  public class UserController : FilterController//Controller
  {
    //
    // GET: /User/
    //private IUserService _userService;
    //private IUserService UserService
    //{
    //  get { return _userService ?? (_userService = new UserService()); }
    //  set { _userService = value; }
    //}
    private IUserService UserService { get; set; }
    //[CheckLogin]
    public ActionResult Index()
    {
      return Content("OK");
    }

  }
}

下面我们对比两种方法的优缺点

      Filter定义过程比较复杂,效率也稍低些,但是却可以对每一个Action进行单独的过滤,同一Action也可以有多条过滤信息,使用比较灵活。

      Controller定义更为简便,效率高,但是却只能对整个Controller中所有方法进行过滤,同一Controller也不太容易有多个Controller过滤父类。

     综上所述,实际项目中大多需求都是同一Controller下所有方法都需要完成登陆验证,所以其实使用Controller过滤更为高效,应对复杂需求时,灵活混用两种方法也不失为一种好的策略。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

ASP.NET 水晶报表打印功能实现代码

ASP.NET下的水晶报表打印,据我所知有以下几种办法可以打印
收藏 0 赞 0 分享

ASP.Net 图片存入数据库的实现代码

在很多时候,我们有这样的需求:把图片存入到数据库当中。在一些应用程序中,我们可能有一些敏感的资料,由于存储在文件系统(file system)中的东西,将很容易被某些用户盗取,所以这些数据不能存放在文件系统中。
收藏 0 赞 0 分享

让Silverlight 2.0动画动起来Making Silverlight 2.0 animation Start(不能运动原因)

Microsoft Expression Blend 2 制作动画个人感觉倒像3DMAX 可以自动捕捉关键帧
收藏 0 赞 0 分享

asp.net Reporting Service在Web Application中的应用

由于我们这个项目中使用微软的报表服务(Reporting Services)作为报表输出工具,本人也对它进行一点点研究,虽没有入木三分,但这点知识至少可以在大部分Reporting Service的场景中应用。
收藏 0 赞 0 分享

C# 文件上传 默认最大为4M的解决方法

.net中默只能上传小于4m的文件,大于4M将无法显示页面.那么如何设置来使imputfile能上传更大的文件呢
收藏 0 赞 0 分享

asp.net 购物车实现详细代码

asp.net 购物车实现详细代码
收藏 0 赞 0 分享

asp.net repeater实现批量删除时注册多选框id到客户端

repeater批量删除时注册多选框id到客户端的实现代码
收藏 0 赞 0 分享

asp.net aspnetpager分页统计时与实际不符的解决办法

最近分页方面根据实际需要修改了一些函数
收藏 0 赞 0 分享

iis 服务器应用程序不可用的解决方法

访问页面时提示 服务器应用程序不可用,大家可以按照下面的方法重新注册下,应该能好点
收藏 0 赞 0 分享

asp.net button 绑定多个参数

asp.net button 绑定多个参数的代码
收藏 0 赞 0 分享
查看更多