愛上MVC~AuthorizeAttribute驗證不通過如何停止當前上下文

來源:互聯網
上載者:User

標籤:

回到目錄

我們知道mvc裡有一些過濾器,AuthorizeAttribute用來做授權,一般在使用者授權方面可以使用它,當使用沒有登陸,我們直接跳到登陸頁,這是沒有問題的,可我要說的是,當使用者對某個Action沒有許可權時,如何禁止對當前action的執行,這個聽起來很不可思議,因為我們一般感覺,當AuthorizeAttribute驗證不通過後,它的當前action也不會被執行,可事實並非如此!

看下面代碼
        public override void OnAuthorization(AuthorizationContext filterContext)        {            #region 例外            bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), inherit: true) ||                                     filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), inherit: true) ||                                     filterContext.RequestContext.HttpContext.Request.Url.Host == "localhost";            if (skipAuthorization)                return;            #endregion            //當前為正常頁面,不是分布視圖            var isValid = false;            //目前使用者的菜單和許可權            var menuAuthority = Lind.DDD.Utils.SerializeMemoryHelper.DeserializeFromJson<List<Tuple<int, string, int>>>(CurrentUser.ExtInfo);            //當前控制器對應的許可權值            var controllerName = filterContext.RouteData.Values["controller"].ToString();            var actionName = filterContext.RouteData.Values["action"].ToString();            //當前許可權,先找完全符合的,如果沒有,再找controller匹配的            var current = menuAuthority.Find(i => !string.IsNullOrWhiteSpace(i.Item2)                && i.Item2.ToLower() == ("/" + controllerName + "/" + actionName).ToLower());            if (current != null)            {                if ((current.Item3 & (int)Authority) == (int)Authority)                {                    isValid = true;                }            }            if (!isValid)            {                string returnUrl = filterContext.RequestContext.HttpContext.Request.UrlReferrer == null ? "/AdminCommon/LogOn" : filterContext.RequestContext.HttpContext.Request.UrlReferrer.AbsolutePath;                filterContext.RequestContext.HttpContext.Response.Write("<div style=‘text-align:center‘><div style=‘MARGIN-RIGHT: auto;MARGIN-LEFT: auto;width:300px;min-height:150px;border: 2px dashed #aaa;color: red; font-size: 14px;padding: 5px;text-align: center;vertical-align:middle;‘><h2>警告</h2><p>您沒有被授權此操作,請<a href=" + returnUrl + ">單擊返回</a></p><p style=‘color:#000‘>時間:" + DateTime.Now + "</p></div></div>");                filterContext.RequestContext.HttpContext.Response.End();                filterContext.Result = new EmptyResult();//清空當前Action,不執行當前Action代碼            }        }

上面代碼是大叔在進行許可權設計時用到的,請注意最後一句EmptyResult,這個方法表示返回一個空的Actioin的結果,只有加上這個空結果,你的當前Action才不會被執行,大叔覺得,這是一種架構設計的新思想,像沒多架構都使用了這種Null 物件的技術,Null 物件即什麼事件都不做,但它並不是null!

  // 摘要:    //     表示一個不執行任何操作的結果,如不返回任何內容的控制器操作方法。    public class EmptyResult : ActionResult    {        // 摘要:        //     初始化 System.Web.Mvc.EmptyResult 類的新執行個體。        public EmptyResult();        // 摘要:        //     執行指定的結果上下文。        //        // 參數:        //   context:        //     結果上下文。        public override void ExecuteResult(ControllerContext context);    }

感覺各位對大叔的支援!

回到目錄

愛上MVC~AuthorizeAttribute驗證不通過如何停止當前上下文

聯繫我們

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