OnActionExecuting驗證用戶登錄


代碼

using Common;
using Service;
using Service.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;

namespace WebPage.Controllers
{
    public class BaseController : Controller
    {
        //public log4net.Ext.IExtLog log = log4net.Ext.ExtLogManager.GetLogger("dblog");

        #region 公用變量
        /// <summary>
        /// 查詢關鍵詞
        /// </summary>
        public string keywords { get; set; }
        /// <summary>
        /// 視圖傳遞的分頁頁碼
        /// </summary>
        public int page { get; set; }
        /// <summary>
        /// 視圖傳遞的分頁條數
        /// </summary>
        public int pagesize { get; set; }
        /// <summary>
        /// 用戶容器,公用
        /// </summary>
        public IUserInfoManage UserInfoManage = Spring.Context.Support.ContextRegistry.GetContext().GetObject("Service.UserInfoManage") as IUserInfoManage;
        #endregion

        #region 用戶對象
        /// <summary>
        /// 獲取當前用戶對象
        /// </summary>
        public Account CurrentUser
        {
            get
            {
                //從Session中獲取用戶對象
                if (SessionHelper.GetSession("CurrentUser") != null)
                {
                    return SessionHelper.GetSession("CurrentUser") as Account;
                }
                //Session過期 通過Cookies中的信息 重新獲取用戶對象 並存儲於Session中
                var account = UserInfoManage.GetAccountByCookie();
                SessionHelper.SetSession("CurrentUser", account);
                return account;
            }
        }
        #endregion

        #region 登錄驗證 OnActionExecuting
        /// <summary>
        /// 重寫控制器 OnActionExecuting(ActionExecutingContext filterContext)方法 實現登錄驗證和公共變量的獲取
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            #region 登錄用戶驗證
            //1、判斷Session對象是否存在
            if (filterContext.HttpContext.Session == null)
            {
                filterContext.HttpContext.Response.Write("<script type='text/javascript'> alert('~登錄已過期,請重新登錄');window.top.location='/'; </script>");
                filterContext.RequestContext.HttpContext.Response.End();
                filterContext.Result = new EmptyResult();
                return;
            }
            //2、登錄驗證
            if (this.CurrentUser == null)
            {
                filterContext.HttpContext.Response.Write("<script type='text/javascript'> alert('登錄已過期,請重新登錄'); window.top.location='/';</script>");
                filterContext.RequestContext.HttpContext.Response.End();
                filterContext.Result = new EmptyResult();
                return;
            }
            #endregion

            #region 公共Get變量
            //分頁頁碼
            object p = filterContext.HttpContext.Request["page"];
            if (p == null || p.ToString() == "") { page = 1; } else { page = int.Parse(p.ToString()); }

            //搜索關鍵詞
            string search = filterContext.HttpContext.Request.QueryString["Search"];
            if (!string.IsNullOrEmpty(search)) { keywords = search; }
            //顯示分頁條數
            string size = filterContext.HttpContext.Request.QueryString["example_length"];
            if (!string.IsNullOrEmpty(size) && System.Text.RegularExpressions.Regex.IsMatch(size.ToString(), @"^\d+$"))
            {
                pagesize = int.Parse(size.ToString());
            }
            else
            {
                pagesize = 10;
            }
            #endregion
        }
        #endregion

        //public void WriteLog(string Operator,string Msg, Common.Enums.enumLog4net level)
        //{
        //    log.Error(Utils.GetIP(), this.CurrentUser.LogName, HttpContext.Request.Url.ToString(), "Module/Detail", Msg);
        //}
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM