亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

mvc過濾器學習

Original 2016-11-02 16:38:51 719
abstract:mvc 過濾器結構圖AuthorizeAttributeAuthorizeAttribute是IAuthorizationFilter的默認實現,添加了Authorize特性的Action將對用戶進行驗證授權,只有通過了用戶才可以進入這個Action.AuthorizeAttribute提供了四種操作方法,打開.net reflector查看源碼1.在進入Action之前首先執(zhí)行OnAuthori

mvc 過濾器結構圖

1.png

AuthorizeAttribute

AuthorizeAttribute是IAuthorizationFilter的默認實現,添加了Authorize特性的Action將對用戶進行驗證授權,只有通過了用戶才可以進入這個Action.

AuthorizeAttribute提供了四種操作方法,打開.net reflector查看源碼

1.在進入Action之前首先執(zhí)行OnAuthorization

public virtual void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }
            if (OutputCacheAttribute.IsChildActionCacheActive(filterContext))
            {
                throw new InvalidOperationException(MvcResources.AuthorizeAttribute_CannotUseWithinChildActionCache);
            }
            if (!filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) && !filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))
            {
                if (this.AuthorizeCore(filterContext.HttpContext))
                {
                    HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
                    cache.SetProxyMaxAge(new TimeSpan(0L));
                    cache.AddValidationCallback(new HttpCacheValidateHandler(this.CacheValidateHandler), null);
                }
                else
                {
                    this.HandleUnauthorizedRequest(filterContext);
                }
            }
        }

2.在OnAuthorization內部會調用AuthorizeCore

protected virtual bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }
            IPrincipal user = httpContext.User;
            if (!user.Identity.IsAuthenticated)
            {
                return false;
            }
            if ((this._usersSplit.Length > 0) && !this._usersSplit.Contains<string>(user.Identity.Name, StringComparer.OrdinalIgnoreCase))
            {
                return false;
            }
            if ((this._rolesSplit.Length > 0) && !this._rolesSplit.Any<string>(new Func<string, bool>(user.IsInRole)))
            {
                return false;
            }
            return true;
        }

默認實現,不僅判斷是否授權,還可以具體到某一個角色,某一個用戶,但這些角色是哪里來的呢?

(1)使用membership框架

(2)使用微軟最新的Identity框架

(3)自定義一個類繼承RoleProvider,并設置配置文件

<roleManager>
      <providers>
        <clear/>
        <add name="MyRoleProvider" type="Filter.Controllers.MyRoleProvider" />
      </providers>
    </roleManager>

3.AuthorizeCore返回一個布爾值,表示是否通過授權,未通過將執(zhí)行HandUnauthorizedRequest返回配置文件forms-loginUrl指向的頁面

        protected virtual void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            filterContext.Result = new HttpUnauthorizedResult();
        }

4.OnAuthorization在緩存模塊請求授權時調用

protected virtual HttpValidationStatus OnCacheAuthorization(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }
            if (!this.AuthorizeCore(httpContext))
            {
                return HttpValidationStatus.IgnoreThisRequest;
            }
            return HttpValidationStatus.Valid;
        }

ActionFilterAttribute

ActionFilterAttribute在mvc中沒有默認實現,需要自己定義

1.OnActionExecuting在OnAuthorization之后(實際上ActionFilter中的方法都在其之后)進入Action之前執(zhí)行

2.OnActionExecuted在Action中所有語句都執(zhí)行完之后執(zhí)行

3.OnResultExecuting在執(zhí)行操作結果(返回繼承自ActionResult的所有類型)前調用

4.OnResultExecuted在執(zhí)行操作結果(返回繼承自ActionResult的所有類型)后調用

public class MyActionFilterAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// 在執(zhí)行操作方法之前由 ASP.NET MVC 框架調用。
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
       
        }
        /// <summary>
        /// 在執(zhí)行操作方法后由 ASP.NET MVC 框架調用。
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {

        }
        /// <summary>
        /// 在執(zhí)行操作結果之前由 ASP.NET MVC 框架調用。
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
     
        }
        /// <summary>
        /// 在執(zhí)行操作結果后由 ASP.NET MVC 框架調用。
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {

        }

    }

HandleErrorAttribute

HandleErrorAttribute是IException的默認實現,在調式的時候,出現異常就會蹦出那黃色的頁面

我們也可以自定義異常處理

需要注意的是ExceptionHandled表示這個異常是否已經處理(可能程序中有多個異常過濾器)

public class MyExceptionAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            if (!filterContext.ExceptionHandled)//避免重復處理
            {
                //獲取拋出異常的對象  
                Exception ex = filterContext.Exception;
                //寫入異常日志

                //已處理 
                filterContext.ExceptionHandled = false;
            }
        }
    }

IAuthenticationFilter

IAuthenticationFilter是認證過濾器接口,他提供了兩個操作方法

1.OnAuthentication在所有過濾器執(zhí)行前執(zhí)行(包括授權過濾器)

2.OnAuthenticationChallenge在OnResultExecuting前執(zhí)行


Release Notes

Popular Entries