1、写个类LoginAuthorityAttribute,继承自AuthorizeAttribute
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace PowerBIDocs.Web.Utils { public class LoginAuthorityAttribute : AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { if (App_Start.GlobalConfig.LoginedUser == null) { filterContext.HttpContext.Response.Redirect("~/Home/Login"); } } } }
2、在所有需要登陆才能访问的控制器中的方法上面,标注: [LoginAuthority]
[LoginAuthority] public ActionResult Logout() { HttpContext.Session[App_Start.GlobalConfig.LoginedUserSessionKey] = null; return RedirectToAction("Login"); }
3、说明:上面的例子中,用户信息存在于SESSION中。