MVC 简单的过滤器
过滤器:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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); } }