標籤:style blog http io ar color sp on div
在asp.net時代,我們通常需要在背景每個頁面進行判斷使用者是否登入的狀態,不管是通過session還是通過windows身分識別驗證還是表單驗證方式等等方法來對使用者登入進行判斷跳轉。但是在mvc時代,我們就沒必要這麼麻煩了。我們可以通過設定其過濾器的方式實現。
首先,我們需要寫一個自訂個control類。重寫父類的OnActionExecuting方法。
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace YPF.CMS.UI.Controllers{ public class BaseController : Controller { // // GET: /Base/ /// <summary> /// 自訂校正 session /// </summary> /// <param name="filterContext"></param> protected override void OnActionExecuting(ActionExecutingContext filterContext) { //在過濾器上下文中判斷是否存在登入後的session對象,沒有就進行跳轉 if (Session["UserInfo"]==null) { filterContext.HttpContext.Response.Redirect("/Login/index"); } base.OnActionExecuting(filterContext); } }}
其次,使其他的控制器全部繼承該類。
最後,我們就在實現了在頁面請求來了之後,會先進入過濾器進行判斷。這裡我們只進行了session的判斷。當然也可以其他的判斷和設定。
企業管理系統開發筆記(4)---後台登入_MVC過濾器