企業管理系統開發筆記(4)---後台登入_MVC過濾器

來源:互聯網
上載者:User

標籤: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過濾器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.