博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC Filter自定义验证(拦截)
阅读量:4600 次
发布时间:2019-06-09

本文共 4065 字,大约阅读时间需要 13 分钟。

1 namespace QS.Web.Extensions 2 { 3     ///  4     ///     验证session、权限    状态 5     ///  6     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] 7     public class RequestFilterAttribute : ActionFilterAttribute 8     { 9         public override void OnActionExecuting(ActionExecutingContext filterContext)10         {11             FilterAttributesInfo attributes = filterContext.GetExecutingContext();12 13             switch (attributes.Action.ToUpper())14             {15                 case "LOGIN":16                 case "LOGINVALID":17                 case "LOGOUT": break;18                 default:19                     //session验证20                     var sessionUserInfo = filterContext.HttpContext.Session[SystemConsts.AdminSession.ToString()];21                     if (null == sessionUserInfo)22                     {23                         var url = new UrlHelper(filterContext.RequestContext);24                         var routeUrl = url.Action("Login", "Account", new { ErrorMsg = "用户信息丢失!" });25                         filterContext.Result = new RedirectResult(routeUrl);26                     }27                     else28                     {29                         //参数非空验证30                         foreach (var param in attributes.ParameterArray)31                         {32                             param.ParameterName.CheckNotNullOrEmpty(param.ParameterName);33                         }34                         //权限验证35                         var permissions = filterContext36                                             .HttpContext37                                             .Session[SystemConsts.AdminRolePermissions.ToString()]38                                             as List
;39 if (!permissions.Any(x =>40 x.ControllerName.ToLower() == attributes.Controller.ToLower() &&41 x.ActionName.ToLower() == attributes.Action.ToLower()))42 {43 filterContext.Result = new ContentResult() { Content = "invalid operation :no permission" };44 }45 }46 break;47 }48 base.OnActionExecuting(filterContext);49 }50 }51 }
View Code

其中涉及到获取  filterContext的方法类如下:

1 // ----------------------------------------------------------------------- 2 //  
3 // Copyright (c) 2016 QS.Web.Extensions. All rights reserved. 4 //
5 //
谭明超
6 //
2016/8/2 18:37:01
7 // ----------------------------------------------------------------------- 8 9 using System;10 using System.Collections.Generic;11 using System.Linq;12 using System.Web;13 using System.Web.Mvc;14 15 namespace QS.Web.Extensions16 {17 /// 18 /// 互殴去19 /// 20 public class FilterAttributesInfo21 {22 /// 23 /// 控制器名称24 /// 25 public string Controller { get; set; }26 /// 27 /// 方法名称28 /// 29 public string Action { get; set; }30 /// 31 /// route参数32 /// 33 public ParameterDescriptor[] ParameterArray { get; set; }34 35 }36 37 /// 38 /// 获取 filter filterContext的相关属性39 /// 40 public static class FilterAttributeExtension41 {42 /// 43 /// 获取当前filterContext的相关属性44 /// 45 /// 46 ///
47 public static FilterAttributesInfo GetExecutingContext(this ActionExecutingContext filterContext)48 {49 return new FilterAttributesInfo50 {51 Controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,52 Action = filterContext.ActionDescriptor.ActionName,53 ParameterArray = filterContext.ActionDescriptor.GetParameters()54 };55 }56 }57 58 }

 

转载于:https://www.cnblogs.com/Tmc-Blog/p/5737879.html

你可能感兴趣的文章
2016012003+陈琦+散列函数的应用及其安全性
查看>>
Android 状态栏通知Notification、NotificationManager详解
查看>>
Sublime Text 3中使用正则表达式删除空行
查看>>
UIApplicationDelegate协议
查看>>
再谈iOS 7的手势滑动返回功能
查看>>
Jmeter测试dubbo接口填坑
查看>>
python小练——找出指定目录下小于指定字节的文件,输出到文本文件
查看>>
渐渐磨砺--16年11月封闭总结
查看>>
[zz]GDB调试精粹及使用实例
查看>>
数据库的创建和删除
查看>>
【消息队列MQ】各类MQ比较
查看>>
最简单的三层实例【插入据
查看>>
设计模式学习笔记——Prototype原型模式
查看>>
pom.xml里有红叉报错的解决办法
查看>>
Perl last和next的用法区别
查看>>
Selenium 管理 Cookies
查看>>
exceptionfunction[LeetCode]Permutations
查看>>
Linux(2)_常用命令2
查看>>
自定义分页
查看>>
[转]DELPHI——调试(1)
查看>>