日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

MVC源码学习之AuthorizeAttribute

發(fā)布時(shí)間:2025/3/19 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MVC源码学习之AuthorizeAttribute 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

常見的Controller定義方式:

public class HomeController : Controller {public ActionResult Index(){return View();} }

如果對(duì)Action的操作需要權(quán)限管理的話,需要在Controller或者Action上面添加AuthorizeAttribute屬性或者AllowAnonymousAttribute屬性。

AllowAnonymousAttribute的意思很明確,允許匿名訪問。

AuthorizeAttribute類型有幾個(gè)屬性:Roles,Users,TypeId。

當(dāng)用戶請(qǐng)求一個(gè)Action時(shí),會(huì)調(diào)用OnAuthorization方法。那么,我們來(lái)看看這個(gè)方法里面的代碼是什么:

public virtual void OnAuthorization(AuthorizationContext filterContext) {if (filterContext == null){throw new ArgumentNullException("filterContext");}if (OutputCacheAttribute.IsChildActionCacheActive(filterContext)){throw new InvalidOperationException(MvcResources.AuthorizeAttribute_CannotUseWithinChildActionCache);}bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), inherit: true)|| filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), inherit: true);if (skipAuthorization){return;}if (AuthorizeCore(filterContext.HttpContext)){HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;cachePolicy.SetProxyMaxAge(new TimeSpan(0));cachePolicy.AddValidationCallback(CacheValidateHandler, null /* data */);}else{HandleUnauthorizedRequest(filterContext);} }

中間有一段是判斷Action或者Controller是否被屬性AllowAnonymousAttribute描述,如果有,則返回。也就是說(shuō),無(wú)論Action或者是Controller哪一個(gè)上面被定義了[AllowAnonymous]屬性,那么,這個(gè)Action就會(huì)跳過驗(yàn)證。

如果沒有的話,會(huì)判斷函數(shù)AuthorizeCore(filterContext.HttpContext)執(zhí)行的結(jié)果是否為true,這樣,我們進(jìn)一步看函數(shù)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 (_usersSplit.Length > 0 && !_usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase)){return false;}if (_rolesSplit.Length > 0 && !_rolesSplit.Any(user.IsInRole)){return false;}return true; }

邏輯比較清晰,如果用戶沒有登錄,返回false;如果授權(quán)用戶組長(zhǎng)度大于0,但同時(shí)不包含當(dāng)前用戶,返回false;如果授權(quán)角色組長(zhǎng)度大于0,但同時(shí)不包含當(dāng)前用戶所在的任何角色,返回false;符合前面所有的條件,則返回true。

到此,授權(quán)驗(yàn)證的上下邏輯算是比較明了。

問題

實(shí)際操作中出現(xiàn)了疑惑:

如果在Controller那里定義了角色為"Users,Admin"的授權(quán),而在Action處定義其他角色的Authorize,會(huì)怎么樣呢?

測(cè)試思路,自定義一個(gè)CustomAuthorize,override上述兩個(gè)函數(shù)(這兩個(gè)函數(shù)都是virtual的),代碼復(fù)制微軟的源碼,加斷點(diǎn)調(diào)試。(重寫的代碼就不展示了。大部分復(fù)制,小部分微調(diào)下。),控制器代碼如下:

[CustomAuthorize(Roles="Users,Admin")] public class HomeController : Controller {public ActionResult Index(){return View();}[CustomAuthorize(Roles = "AAAAA")]public ActionResult About(){ViewBag.Message = "Your application description page.";return View();} }

調(diào)試模式啟動(dòng)Home下面的About頁(yè)面,在OnAuthorization里面加斷點(diǎn),可以看到如下數(shù)據(jù):
1. 在filterContext.ActionDescriptor和filterContext.ActionDescriptor.ControllerDescriptor里面可以看到各自的Roles內(nèi)容
2. base.Roles的值為"Users,Admin"。

由此,我們可以得出:

當(dāng)Controller和Action都出現(xiàn)了角色授權(quán)時(shí),多個(gè)授權(quán)不是合并,也不是取Action上面的,而是直接取Controller上面定義的角色,繼而判斷當(dāng)前用戶是否有授權(quán)。

?

Users是不是也是這樣的,有興趣的童鞋可以自己調(diào)試看看o(∩_∩)o ~

轉(zhuǎn)載于:https://www.cnblogs.com/icyJ/p/MVC_Authorize.html

總結(jié)

以上是生活随笔為你收集整理的MVC源码学习之AuthorizeAttribute的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。