public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { RBACContext.SetRBACUser(u => { return new RBACUser { UserName = u, Roles = new List<RBACRole> { new RBACRole { RoleName=, Permissions = new List<RBACPermission> { new RBACPermission { PermissionName= } } } } }; }); DelegeteRoleProvider.SetGetRolesForUser(userName => RBACContext.GetRBACUser(userName).Roles.SelectMany(o => o.Permissions).Select(p => p.PermissionName).ToArray()); DelegeteRoleProvider.SetIsUserInRole((userName, roleName) => RBACContext.GetRBACUser(userName).Roles.SelectMany(o => o.Permissions).Any(p => p.PermissionName == roleName)); AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTable.Routes); } }
5.ÔÚASP.NET MVCÖÐͨ¹ý.NET APIʹÓÃUser.IsInRoleºÍAuthorizeAttribute´Ëʱ¶¼¿ÉÒÔʹÓã¬ÎÒÃÇÒѾÍê³ÉÁËÒ»¸öRBACȨÏÞÖмä²ã£¬¼´¸ôÀëÁ˲»Í¬ÏµÍ³µÄ¾ßÌåʵÏÖ£¬Ò²²»ÓÃʹÓÃеÄAPIµ÷Óá£Èç¹ûÊÇ·þÎñ²ã£¬Ê¹ÓÃThread.CurrentPrincipal.IsInRoleºÍPrincipalPermissionAttribute¡£
namespace RBACExample.Controllers { public class HomeController : Controller { public ActionResult Login(string returnUrl) { FormsAuthentication.SetAuthCookie(, false); return Redirect(returnUrl); } public ActionResult Logoff() { FormsAuthentication.SignOut(); ); } public ActionResult Index() { ); } [Authorize] public ActionResult Account() { , User.Identity.IsAuthenticated)); } [Authorize(Roles = )] public ActionResult Admin() { , User.IsInRole())); } } }
6.À©Õ¹AuthorizeAttribute£¬Í³Ò»ÅäÖÃÊÚȨAuthorizeAttributeµÄʹÓý«ÊÚȨ·ÖÉ¢ÔÚ¶à¸öControllerÖУ¬ÎÒÃÇ¿ÉÒÔÀ©Õ¹AuthorizeAttribute£¬×Ô¶¨ÒåÒ»¸öMvcAuthorizeAttribute£¬ÒÔ¾²Ì¬×ֵ䱣´æÅäÖã¬ÕâÑù¾Í¿ÉÒÔͨ¹ý´úÂë¡¢ÅäÖÃÎļþ»òÊý¾Ý¿âµÈ·½Ê½¶ÁÈ¡ÅäÖÃÔÙ´æ·Åµ½×ÖµäÖУ¬ÊµÏÖ¶¯Ì¬ÅäÖᣴËʱ¿ÉÒÔ´ÓControllerÖÐÒƳýAuthorizeAttribute¡£ÈçÇ°ÎÄËùÊö£¬¿Í»§¶ËµÄ·ÃÎÊ¿ØÖÆÓëȨÏÞµÄÆ¥ÅäÓ¦¸Ã´æ´¢µ½¿Í»§¶ËΪ×î¼Ñ£¬¼´Ê¹´æ·Åµ½Êý¾Ý¿âÒ²²»Òª¹ØÁªÈ¨ÏÞÏà¹ØµÄ±í¡£
namespace RBACExample.RBAC { public class MvcAuthorizeAttribute : AuthorizeAttribute { private static Dictionary<string, string> _ActionRoleMapping = new Dictionary<string, string>(); AddConfig(string controllerAction, params string[] roles) { var rolesString = string.Empty; roles.ToList().ForEach(r => rolesString += + r); rolesString = rolesString.TrimStart(); _ActionRoleMapping.Add(controllerAction, rolesString); } OnAuthorization(AuthorizationContext filterContext) { , filterContext.ActionDescriptor.ControllerDescriptor.ControllerName, filterContext.ActionDescriptor.ActionName); if (_ActionRoleMapping.ContainsKey(key)) { this.Roles = _ActionRoleMapping[key]; base.OnAuthorization(filterContext); } } } }
ͨ¹ýGlobalFilterCollectionÅäÖý«MvcAuthorizeAttributeÅäÖÃΪȫ¾ÖFilter¡£
RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); MvcAuthorizeAttribute.AddConfig(); MvcAuthorizeAttribute.AddConfig(, Permission.AdminPermission); filters.Add(new MvcAuthorizeAttribute()); }
7.°´ÐèÉè¼ÆʵÌåÀ൱RBACÄ£ÐͲ»Ö±½ÓÒÀÀµÊµÌåÀàʱ£¬ÊµÌåÀà¿ÉÒÔ°´ÐèÉè¼Æ£¬²»ÔÙÐèҪΪÁËǨ¾ÍRBACµÄ¹ØÁªÒýÈë¹ý¶àµÄʵÌ壬¿ÉÒÔÕæÕý×öµ½¾ßÌåÎÊÌâ¾ßÌå·ÖÎö£¬²»ÐèҪʲôϵͳ¶¼ÉÏRole¡¢PermissionµÈʵÌåÀ࣬¶ÔÓÚ½ÇÉ«Îȶ¨µÄϵͳ£¬¼È¼õÉÙÁËϵͳµÄ¸´ÔӶȣ¬Ò²¼õÉÙÁË´óÁ¿ºǫ́µÄ¹¦ÄÜʵÏÖ£¬Ò²¼ò»¯Á˺ǫ́µÄ²Ù×÷£¬²»ÓÃʲôϵͳ¶¼ÉÏÒ»Ì×Óû§Í·ÌÛÅàѵÈËԱҲͷÌÛµÄȨÏÞÖÐÐÄ¡£
(1)ʹÓÃÊôÐÔÅжÏȨÏÞµÄϵͳ
ÓÐЩϵͳ£¬±ÈÈç¸öÈ˲©¿Í£¬Ö»ÓÐÒ»¸ö¹ÜÀíÔ±½ÇÉ«admin£¬admin½ÇÉ«ÊÇÎȶ¨µÄȨÏÞ²»±äµÄ£¬ËùÒԼȲ»ÐèÒª¿¼ÂÇʹÓöà¸ö½ÇÉ«Ò²²»ÐèÒªÔÙ½øÐÐȨÏÞ³éÏó£¬Òò´ËʹÓÃUser.IsAdminÊôÐÔ´úÌæRoleºÍPermission¾Í¿ÉÒÔ£¬Ã»±ØÒªÔÙʹÓÃRoleºÍPermissionʵÌåÀ࣬Ôö´ó´úÂëÁ¿¡£ºǫ́½øÐÐȨÏÞ¹ÜÀíÖ»ÐèҪʵÏÖÊôÐԵı༡£
RBACContext.SetRBACUser(u => { , IsAdmin = true }; var rbacUser = new RBACUser { UserName = user.UserName }; if (user.IsAdmin) { rbacUser.Roles.Add(new RBACRole { RoleName = , Permissions = new List<RBACPermission> {new RBACPermission { PermissionName= } } }); } return rbacUser; });
(2)ʹÓýÇÉ«ÅжÏȨÏÞµÄϵͳ
¡¡