MVC SSO登陸 的麻煩事~

來源:互聯網
上載者:User

標籤:

前段時間用MVC + Redis 做session做了個簡單的單點Web站。真是日了狗的問題多。

今天正好睡不著,做個備忘筆記>_<

實現方法很簡單,無非就是從重載個Controller或 做一個ActionFilterAttribute就可以達到目的。

下面貼一個Controller的代碼實現,ActionFilterAttribute實現方式類似:

這裡我為了圖方便用了servicestack.redis 雖然最新的免費版本 有很大的效能限制(真坑爹)。

兩個Controller 類,Base用於在Action執行前填充的Account資訊,一個要求必須登陸,否則調到SSO

    
/// <summary> /// 為所有Action填充Account使用者資訊 /// </summary>

  public class BaseAccountController : Controller { protected RedisHelper.RedisHelper redisHelper { get; private set; } protected readonly static String TokenKeyCookie = "QTMAccountTokenCookie"; public BaseAccountController() { redisHelper = new RedisHelper.RedisHelper(); } ~BaseAccountController() { if (redisHelper != null) { redisHelper.Dispose(); } } protected override void OnActionExecuting(ActionExecutingContext filterContext) { var cookie = filterContext.HttpContext.Request.Cookies[TokenKeyCookie]; String token = filterContext.HttpContext.Request.QueryString["Token"]; if (cookie != null) { token = cookie.Value; var accountIndex = redisHelper.Get<AccountIndex>(token); ViewBag.CurrentAccount = accountIndex; } else if (!String.IsNullOrWhiteSpace(token)) { var accountIndex = CreateTokenCookie(token); ViewBag.CurrentAccount = accountIndex; } } /// <summary> /// 建立Token令牌的本地Cookie /// </summary> /// <param name="Token">Token令牌</param> /// <returns></returns> protected AccountIndex CreateTokenCookie(string Token) { var accountIndex = redisHelper.Get<AccountIndex>(Token); //判斷是否 為有效Token令牌 if (accountIndex == null) { return null; } //生產Token令牌的Cookie var token_cookie = new HttpCookie(TokenKeyCookie, Token) { HttpOnly = true, Secure = FormsAuthentication.RequireSSL, Path = "/", Expires = DateTime.Now.AddYears(10) }; HttpContext.Response.Cookies.Add(token_cookie); return accountIndex; }

在

    /// <summary>    /// 控制器的 所有Action必須 持有 有效 Account Token令牌。    /// </summary>    public class AccountAuthenController : BaseAccountController    {        public AccountAuthenController()            : base()        {        }        ~AccountAuthenController()        {            if (redisHelper != null)            {                redisHelper.Dispose();            }        }        protected override void OnActionExecuting(ActionExecutingContext filterContext)        {            String token = filterContext.HttpContext.Request.QueryString["Token"];            var cookie = filterContext.HttpContext.Request.Cookies[TokenKeyCookie];            if (cookie == null)            {                if (!String.IsNullOrWhiteSpace(token))                {                    //當有返回新的Token令牌時,建立一個新的Token本地Cookie                    var accountIndex = CreateTokenCookie(token);                    if (accountIndex == null)                    {                        filterContext.HttpContext.Response.Redirect(String.Format(ActionUri.Login, filterContext.HttpContext.Request.Url.AbsoluteUri));                    }                }                else                {                    filterContext.HttpContext.Response.Redirect(String.Format(ActionUri.Login, filterContext.HttpContext.Request.Url.AbsoluteUri));                }            }            else            {                token = cookie.Value;                var accountIndex = redisHelper.Get<AccountIndex>(token);                if (accountIndex == null)                {   //無效的Token Cookie                    filterContext.HttpContext.Response.Cookies[TokenKeyCookie].Expires = DateTime.Now.AddYears(-1);                    filterContext.HttpContext.Response.Redirect(String.Format(ActionUri.Login, filterContext.HttpContext.Request.Url.AbsoluteUri));                }            }            base.OnActionExecuting(filterContext);        }        #region Private Method        #endregion    }

 

MVC SSO登陸 的麻煩事~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.