手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >MVC使用Controller代替Filter完成登录验证(Session校验)学习笔记5
MVC使用Controller代替Filter完成登录验证(Session校验)学习笔记5
摘要:之前的学习中,在对Session校验完成登录验证时,通常使用Filter来处理,方法类似与前文的错误日志过滤,即新建Filter类继承Act...

之前的学习中,在对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过滤更为高效,应对复杂需求时,灵活混用两种方法也不失为一种好的策略。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持查字典教程网。

【MVC使用Controller代替Filter完成登录验证(Session校验)学习笔记5】相关文章:

asp.net Timer的使用方法

WebService出现"因 URL 意外地以 结束,请求格式无法识别"的解决方法

垃圾代码二三行 ASPX小马

Asp.Net中NHiernate的Session的管理

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

C#设置本地网络如DNS、网关、子网掩码、IP等等

如何将DataSet转换成josn并输出

将文本文件的内容或者文字保存成图片的方法介绍

用javascript css实现GridView行背景色交替、鼠标划过行变色,点击行变色选中

.net的常规命名规范示例

精品推荐
分类导航