• 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")]

  • 相关阅读:
    滑动窗口法与剑指offer:和为S的连续正数数列 与 和为S的两个数字
    数组中的逆序对与归并中的分治思想
    重读STL源码剖析:迭代器
    重读深度探索C++对象模型:函数
    FreeMarker笔记 前言&第1章 入门
    分享我的PL/SQL的优化设置,为开发全面提速
    迅影QQ视频查看v2.0 源码
    Invalid encoding name "UTF8". 报错 XML
    [HNOI2003]消防局的设立
    We need water!
  • 原文地址:https://www.cnblogs.com/GoCircle/p/6409767.html
Copyright © 2020-2023  润新知