示例#1
0
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string ActionName     = filterContext.ActionDescriptor.ActionName;
            string ControllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;

            string        User      = filterContext.HttpContext.User.Identity.GetUserId();
            List <string> UserRoles = (List <string>)System.Web.HttpContext.Current.Session["Roles"];

            try
            {
                if (User != null && !UserRoles.Contains("Admin") && ControllerName != "Account" && ControllerName != "Menu")
                {
                    if (!ValidateData.ValidateUserPermission(ActionName, ControllerName))
                    {
                        return;
                    }
                    else
                    {
                        throw new Exception("Permission Denied");
                    }
                }
            }
            catch (Exception ex)
            {
                var model = new HandleErrorInfo(ex, ControllerName, ActionName);
                if (filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
                {
                    filterContext.Result = new ViewResult
                    {
                        ViewName = "AjaxError",
                        ViewData = new ViewDataDictionary <HandleErrorInfo>(model),
                        TempData = filterContext.Controller.TempData
                    };
                }
                else
                {
                    filterContext.Result = new ViewResult
                    {
                        ViewName = "Error",
                        ViewData = new ViewDataDictionary <HandleErrorInfo>(model),
                        TempData = filterContext.Controller.TempData
                    };
                }
            }

            // Don't show filter multiple times when using Html.RenderAction or Html.Action.
            if (filterContext.IsChildAction == true)
            {
                return;
            }

            // Action trace centralyzed
            //ApplicationTracer.ApplicationTrace(System.Diagnostics.TraceLevel.Info, this.GetType(), filterContext.Controller.ToString(), filterContext.ActionDescriptor.ActionName, "Child action", null);
        }