• C# MVC 页面面包屑以及相应的权限验证操作


    一、特性类

        /// <summary>
        /// 访问权限控制属性。
        /// </summary>
        [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
        public class ActionAttribute : Attribute
        {
            /// <summary>
            /// 初始化Action类的新实例。
            /// </summary>
            public ActionAttribute(string id)
            {
                this.ActionID = id;
            }
    
            /// <summary>
            /// 操作标识。
            /// </summary>
            public string ActionID { get; set; }
        }
    View Code

    二、拦截器代码

            /// <summary>
            /// 全局拦截器
            /// </summary>
            protected override void OnAuthorization(AuthorizationContext filterContext)
            {
                base.OnAuthorization(filterContext);
    
                        Type controllerType = filterContext.Controller.GetType();
                        MethodInfo methodInfo = controllerType.GetMethod(filterContext.ActionDescriptor.ActionName, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
                        Attribute customAttribute = Attribute.GetCustomAttribute(methodInfo, typeof(GTC.Web.Attributes.ActionAttribute));
                        GTC.Web.Attributes.ActionAttribute actionAttribute = customAttribute == null ? null : (GTC.Web.Attributes.ActionAttribute)customAttribute;
    
                    BindCrumbs(actionAttribute.ActionID);
            }
    
    
            /// <summary>
            /// 绑定面包屑
            /// </summary>
            /// <param name="functionKey"></param>
            private void BindCrumbs(string functionKey)
            {
                ArrayList arr = new ArrayList();
    
                var thisAction = GetFunctions().FirstOrDefault(p => p.FActionID == functionKey);
    
                if (thisAction != null)
                {
                    arr.Add(thisAction.FActionName);
    
                    QueryCrumbs(thisAction.FParentActionID, ref arr);
    
                    arr.Reverse();
                    ViewBag.Menu = "当前位置 :" + string.Join("", (string[])arr.ToArray(typeof(string)));
                }
            }
    
            private void QueryCrumbs(string parentId, ref ArrayList arr)
            {
                var parentAction = GetFunctions().FirstOrDefault(p => p.FActionID == parentId);
    
                if (parentAction != null)
                {
                    arr.Add("->");
    
                    if (parentAction.FParentActionID == "100000")
                    {
                        arr.Add(parentAction.FActionName);
                    }
                    else
                    {
                        arr.Add("<a href='" + parentAction.FActionUrl + "'>" + parentAction.FActionName + "</a>");
                        QueryCrumbs(parentAction.FParentActionID, ref arr);
                    }
                }
            }
    
            public static List<Action> GetFunctions()
            {
                 //获取系统所有权限
            }
    View Code

    三、在方法上加标签[Attributes.Action(ActionID = "5201314")]

  • 相关阅读:
    Socket原理与编程基础
    Hello cnblogs
    c# List 分页问题
    chrome下载Word失败问题
    前端时间Date显示问题踩坑
    Vue跳转同一界面多次,使用不同数据进行渲染
    Hadoop在Linux环境下的配置
    RabbitMQ下载安装
    Codeforces 527 C. Glass Carving
    python压缩、解压文件
  • 原文地址:https://www.cnblogs.com/GoCircle/p/6409767.html
Copyright © 2020-2023  润新知