HTML5¼¼Êõ

ASP.NET ϵÁУºRBACȨÏÞÉè¼Æ - #Íõ¸Õ(2)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£º²©¿ÍÔ° 2016-01-30 11:59 ÎÒÒªÆÀÂÛ( )

public class MvcApplication : System.Web.HttpApplication{ protected void Application_Start(){RBACContext.SetRBACUser(u = { return new RBACUser{UserName = u,Roles = new ListRBACRole { new RBACRole{Rol

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)ʹÓýÇÉ«ÅжÏȨÏÞµÄϵͳ

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • HTML5 ½ø½×ϵÁУºÍÏ·Å API ʵÏÖÍÏ·ÅÅÅÐò - _ÁÖöÎ

    HTML5 ½ø½×ϵÁУºÍÏ·Å API ʵÏÖÍÏ·ÅÅÅÐò - _ÁÖöÎ

    2017-05-02 11:02

  • ÈçºÎÔÚ ASP.NET Core Öз¢ËÍÓʼþ - Savorboard

    ÈçºÎÔÚ ASP.NET Core Öз¢ËÍÓʼþ - Savorboard

    2017-05-02 08:02

  • JS×é¼þϵÁСª¡ª×Ô¼º¶¯ÊÖ·â×°bootstrap-treegrid×é¼þ - ÀÁµÃ°²·Ö

    JS×é¼þϵÁСª¡ª×Ô¼º¶¯ÊÖ·â×°bootstrap-treegrid×é¼þ - ÀÁµÃ°²·Ö

    2017-04-28 14:02

  • Ê®¶þ¸ö ASP.NET Core Àý×Ó - Savorboard

    Ê®¶þ¸ö ASP.NET Core Àý×Ó - Savorboard

    2017-04-27 16:01

ÍøÓѵãÆÀ
µ