新建类MemberValidationAttribute.cs并集成此类AuthorizeAttribute
添加如下代码
public class MemberValidationAttribute: AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { //获取Cookies中的user因为登陆时存的键值是user var memberValidation = System.Web.HttpContext.Current.Request.Cookies.Get("user"); //如果memberValidation为null 或者 memberValidation不等于Success if (memberValidation == null || memberValidation.Value == "") { //页面跳转到 登录页面 filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Sign", action = "Index" })); return; } //通过验证 return; } }
如果判断是否登陆就在控制器的前面加 [MemberValidation]