• .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 }
  • 相关阅读:
    ReactNative: 数据请求
    ReactNative: 使用Geolocation的API获取位置信息
    ReactNative: 使用第三方库图像选择器react-native-image-picker和react-native-image-crop-picker
    MDG_TR_DEST
    【VerySky原创】后台JOB运行-相关表
    【VerySky原创】RPR_ABAP_SOURCE_SCAN
    【VerySky原创】 ME9F
    【VerySky原创】如何查找SNRO编号范围的使用情况;
    【VerySky原创】怎样查找到CDHDR、CDPOS表中的OBJECTCLAS字段
    【由VerySky原创】由Number Range 导致凭证生成但无法保存的问题
  • 原文地址:https://www.cnblogs.com/lori/p/2051909.html
Copyright © 2020-2023  润新知