• 基于页面的权限设计原形


    权限属性定义:

    /// <summary>
        /// 权限属性
        /// </summary>
        [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
        public class AccessLevAttribute : Attribute
        {
            /// <summary>
            /// 名称
            /// </summary>
            public string Name { get; set; }
    
            /// <summary>
            /// 权限
            /// </summary>
            public string LevStr { get; set; }
    
            /// <summary>
            /// 
            /// </summary>
            static Type attrType = typeof(AccessLevAttribute);
    
            public AccessLevAttribute(string name)
            {
                this.Name = name;
            }
    
            public AccessLevAttribute(string name, string levStr)
            {
                this.Name = name;
                this.LevStr = levStr;
            }
    
            /// <summary>
            /// 解析类属性
            /// </summary>
            /// <param name="t"></param>
            /// <returns></returns>
            public static AccessLevAttribute ParseClass(Type t)
            {
                return Parse(t.GetCustomAttributes(attrType, false));
            }
    
            /// <summary>
            /// 解析方法属性
            /// </summary>
            /// <param name="m"></param>
            /// <returns></returns>
            public static AccessLevAttribute ParseMethod(MethodInfo m)
            {
                return Parse(m.GetCustomAttributes(attrType, false));
            }
    
            static AccessLevAttribute Parse(object[] attributes)
            {
                return (attributes == null || attributes.Length != 1) ? null : attributes[0] as AccessLevAttribute;
            }
        }
    

    页面基类:

    public class PageBase : System.Web.UI.Page
        {
            public PageBase()
            {
                this.Init += new EventHandler(PageBase_Init);
            }
    
            void PageBase_Init(object sender, EventArgs e)
            {
                Type clssType = this.GetType().BaseType;
    
                var classAttr = AccessLevAttribute.ParseClass(clssType); //获取类上定义的权限数据
                Response.Write(classAttr == null ? clssType.Name : classAttr.Name);
                            
                foreach (var m in clssType.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    var a = AccessLevAttribute.ParseMethod(m); //获取方法上定义的权限数据
                    Response.Write(a == null ? m.Name : a.Name);
                }
                
            }
        }
    

    页面类:

    [AccessLev("classAliasName")]
        public partial class WebForm1 :PageBase
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            [AccessLev("methodAliasName")]
            string Test()
            {
                return DateTime.Now.ToString();
            }
        }
    

    验证在基类中统一完成,相对一般的基于url验证更安全,且可细化到页面的方法级

  • 相关阅读:
    linux系列之-—03 压缩和解压缩命令
    Atlassian Confluence安装
    常见ODBC及OLEDB连接串的写法
    windows 怎么验证域名是否开启了 https
    Jenkins基础篇 系列之-—05 集成JIRA
    SVN系列之—-SVN版本回滚的办法
    JMeter 系列之—-02 创建数据库测试计划
    golang web框架 beego 学习 (一) 环境搭建
    补充 3:Golang 一些特性
    补充 1: panic 、recover、 defer
  • 原文地址:https://www.cnblogs.com/jiang_zheng/p/4632793.html
Copyright © 2020-2023  润新知