MVC授权过滤器

 

public class FilterCenter : IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            //控制器名
            string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            //动作方法名
            string actionName = filterContext.ActionDescriptor.ActionName;
            string[] ControllerList = {"userinfo" };
            string[] ActionList = { "index", "logindo" };

            if (ControllerList.Contains(controllerName.ToLower())&& ActionList.Contains(actionName.ToLower()))
            {

            }
            else
            {
                if (System.Web.HttpContext.Current.Session["name"] == null)
                {
              
                    ContentResult res = new ContentResult();
                    res.Content = "<script>alert('请先登录后再进行查看');location.href='/userinfo/index';</script>";
                    filterContext.Result = res;
                }
            }
        }
    }

 

posted @ 2017-11-21 16:54  李笙楠  阅读(188)  评论(0编辑  收藏  举报