创建一个AuthenticateFilterAttribute(即过滤器/拦截器)
引用System.Web.Mvc;
public class AuthenticateFilterAttribute: ActionFilterAttribute,IActionFilter { public override void OnActionExecuting(ActionExecutingContext filterContext) { // Action或Controller上定义了[AllowAnonymousAttribute] var hasAllowAnymous = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true); if (hasAllowAnymous) return; // 不需要登录,则通过。 var session = filterContext.HttpContext.Session; if (session == null) return; //下面就是写过滤的条件,一般来说就是判断session是否过期,跳转到登陆页面。 //以及判断角色,身份 } }