新增一個 FilterController 繼承 Controller,判斷 Session是否存在,否的話導回登入頁或其他目標頁。
public class FilterController : Controller
{
dbMyCompanyContext myHR = new dbMyCompanyContext();
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
if (HttpContext.Session.GetObject<TUser>(CDictionary.Current_User) == null)
filterContext.Result = RedirectToAction("Login","Login");
}
}