.NET MVC中的过滤器

简介:

过滤器其实就是.net中的特性,在.net.35之后我们可以在类或者方法名称上面加某种特性,而在.net mvc环境下,有几个比较重要的特性,如AuthorizeAttribute

它主要有在权限验证上,有时我们习惯叫它“过滤器”,原因是它可以把不符合要求的用户过滤掉,呵呵,下面是系统中常见的用户权限过滤器的代码,供大家学习

 1 namespace Web.Attributes
 2 {
 3 
 4     /// <summary>
 5     /// 用户验证列举
 6     /// </summary>
 7     public enum AuthenticationType
 8     {
 9         /// <summary>
10         /// 登录
11         /// </summary>
12         Login,
13         /// <summary>
14         /// 后台登陆
15         /// </summary>
16         BackgroundLogin,
17         /// <summary>
18         /// 注册
19         /// </summary>
20         Register,
21     }
22 
23     /// <summary>
24     /// 用户验证过滤器:前台
25     /// </summary>
26     public class UserAuthentication : AuthorizeAttribute
27     {
28         public AuthenticationType Authentication { get; set; }
29         /// <summary>
30         /// 构造函数
31         /// </summary>
32         public UserAuthentication()
33             : this(AuthenticationType.Login) { }
34         public UserAuthentication(AuthenticationType authentication)
35         {
36             this.Authentication = authentication;
37         }
38         /// <summary>
39         /// 执行前验证
40         /// </summary>
41         public override void OnAuthorization(AuthorizationContext filterContext)
42         {
43 
44             //验证不成功的时候
45             switch (this.Authentication)
46             {
47                 case AuthenticationType.Login:
48                     if (!ClientHelper.Current.HasUserInfo)
49                         filterContext.Result = new RedirectToRouteResult("Default", new RouteValueDictionary { { "Action", "LogOn" }, { "Controller", "Account" }, { "returnUrl", HttpContext.Current.Request.Url.ToString() } });
50                     break;
51 
52                 case AuthenticationType.BackgroundLogin:
53                     if (string.IsNullOrEmpty(SessionAction.ReadSession("Background_Current_UserID")) || Convert.ToInt32(SessionAction.ReadSession("Background_Current_UserID")) < 0)
54                         filterContext.Result = new RedirectToRouteResult("Default", new RouteValueDictionary { { "Action", "LogOn" }, { "Controller", "Account" }, { "returnUrl", HttpContext.Current.Request.Url.ToString() } });
55                     break;
56                 case AuthenticationType.Register:
57                     filterContext.Result = new RedirectToRouteResult("Default", new RouteValueDictionary { { "Action", "Register" }, { "Controller", "Account" } });
58                     break;
59                 default:
60                     filterContext.Result = new RedirectToRouteResult("Default", new RouteValueDictionary { { "Action", "Index" }, { "Controller", "Home" } });
61                     break;
62             }
63 
64         }
65     }
66 }
本文转自博客园张占岭(仓储大叔)的博客,原文链接:.NET MVC中的过滤器 ,如需转载请自行联系原博主。
目录
相关文章
|
3月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
41 0
|
1月前
|
开发框架 前端开发 .NET
进入ASP .net mvc的世界
进入ASP .net mvc的世界
29 0
|
1月前
mvc.net分页查询案例——mvc-paper.css
mvc.net分页查询案例——mvc-paper.css
5 0
|
1月前
|
开发框架 前端开发 .NET
C# .NET面试系列六:ASP.NET MVC
<h2>ASP.NET MVC #### 1. MVC 中的 TempData\ViewBag\ViewData 区别? 在ASP.NET MVC中,TempData、ViewBag 和 ViewData 都是用于在控制器和视图之间传递数据的机制,但它们有一些区别。 <b>TempData:</b> 1、生命周期 ```c# TempData 的生命周期是短暂的,数据只在当前请求和下一次请求之间有效。一旦数据被读取,它就会被标记为已读,下一次请求时就会被清除。 ``` 2、用途 ```c# 主要用于在两个动作之间传递数据,例如在一个动作中设置 TempData,然后在重定向到另
95 5
|
3月前
|
XML 前端开发 定位技术
C#(NET Core3.1 MVC)生成站点地图(sitemap.xml)
C#(NET Core3.1 MVC)生成站点地图(sitemap.xml)
25 0
|
3月前
|
前端开发
.net core mvc获取IP地址和IP所在地(其实是百度的)
.net core mvc获取IP地址和IP所在地(其实是百度的)
123 0
|
8月前
|
存储 开发框架 前端开发
[回馈]ASP.NET Core MVC开发实战之商城系统(五)
经过一段时间的准备,新的一期【ASP.NET Core MVC开发实战之商城系统】已经开始,在之前的文章中,讲解了商城系统的整体功能设计,页面布局设计,环境搭建,系统配置,及首页【商品类型,banner条,友情链接,降价促销,新品爆款】,商品列表页面,商品详情等功能的开发,今天继续讲解购物车功能开发,仅供学习分享使用,如有不足之处,还请指正。
116 0
|
9月前
|
开发框架 前端开发 .NET
[回馈]ASP.NET Core MVC开发实战之商城系统(一)
[回馈]ASP.NET Core MVC开发实战之商城系统(一)
113 0
|
9月前
|
SQL 开发框架 前端开发
[回馈]ASP.NET Core MVC开发实战之商城系统(开篇)
[回馈]ASP.NET Core MVC开发实战之商城系统(开篇)
144 0
|
9月前
|
开发框架 缓存 JSON
ASP.NET Core MVC 从入门到精通之Filter
ASP.NET Core MVC 从入门到精通之Filter
120 0