MVC 简单的过滤器

过滤器:

    public class CheckLoginFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // 1、允许匿名访问 用于标记在授权期间要跳过 AuthorizeAttribute 的控制器和操作的特性 
            var actionAnonymous = filterContext.ActionDescriptor.GetCustomAttributes(typeof(AllowAnonymousAttribute), true) as IEnumerable<AllowAnonymousAttribute>;
            var controllerAnonymous = filterContext.Controller.GetType().GetCustomAttributes(typeof(AllowAnonymousAttribute), true) as IEnumerable<AllowAnonymousAttribute>;
            if ((actionAnonymous != null && actionAnonymous.Any()) || (controllerAnonymous != null && controllerAnonymous.Any()))
            {
                return;
            }

            var sessionData = filterContext.HttpContext.Session["UserInfo"];
            if (sessionData == null)
            {
                filterContext.Result = new RedirectResult("/Login/Login");
                return;
            }

            base.OnActionExecuting(filterContext);
        }
    }
View Code

 

posted @ 2022-05-30 12:38  bxzjzg  阅读(23)  评论(0编辑  收藏  举报