Use ActionFilterAttribute to check user logging
public class CheckLoginAttribute : ActionFilterAttribute { public CheckLoginAttribute() { } public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.HttpContext.Session["User"] == null) { filterContext.Result = new RedirectResult("/Account/LogOn"); return; } base.OnActionExecuting(filterContext); } }
public class HomeController : Controller { public ActionResult Index() { ViewBag.Message = "Welcome to ASP.NET MVC!"; return View(); } [CheckLogin] public ActionResult About() { return View(); } }
ref: MSDN