限制有登入才能導向目標 Controller

新增一個 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");
        }

        
    }

需要限制的 Controller 繼承 FilterController 即可

發佈留言