コード例 #1
0
            public void OnActionExecuting(ActionExecutingContext context)
            {
                //获取action上的特性
                var actionFilter = context.ActionDescriptor.FilterDescriptors.Where(o => o.Scope == FilterScope.Action)
                                   .Select(o => o.Filter).OfType <SysUserPrmAttribute>().FirstOrDefault();

                //如果忽略
                if (actionFilter?.IgnoreFilter ?? _ignoreFilter)
                {
                    return;
                }

                bool result = false;

                var spareFilter = context.ActionDescriptor.EndpointMetadata.OfType <PrmSpareAttribute>().FirstOrDefault();

                if (spareFilter != null)
                {
                    result = _workContext.AuthorityCheck(spareFilter.Name);
                }
                else
                {
                    var route = context.ActionDescriptor.AttributeRouteInfo;

                    if (!String.IsNullOrEmpty(route.Name))
                    {
                        result = _workContext.AuthorityCheck(route.Name);
                    }
                    else
                    {
                        string action     = context.RouteData.Values["action"].ToString();
                        string controller = context.RouteData.Values["controller"].ToString();
                        result = _workContext.AuthorityCheck(action, controller);
                    }
                }
                if (result)
                {
                    return;
                }
                if (context.HttpContext.Request.IsAjaxRequest())
                {
                    context.Result = new JsonResult(new AjaxResult()
                    {
                        Success = false, Message = "没有权限"
                    });
                }
                else
                {
                    context.Result = new ViewResult()
                    {
                        ViewName = "NoPermission"
                    };
                }
            }