zoukankan      html  css  js  c++  java
  • 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")]

  • 相关阅读:
    密码保护
    实现搜索功能
    完成个人中心—导航标签
    个人中心标签页导航
    评论列表显示及排序,个人中心显示
    完成评论功能
    从首页问答标题到问答详情页
    首页列表显示全部问答,完成问答详情页布局。
    JavaScript Array Reduce用于数组求和
    【Angular5】 返回前一页面 go back to previous page
  • 原文地址:https://www.cnblogs.com/GoCircle/p/6409767.html
Copyright © 2011-2022 走看看