HTML5¼¼Êõ

Asp.Net MVCѧϰ×ܽᣨÈý£©¡ª¡ª¹ýÂËÆ÷ÄãÔõô¿´£¿ - SeeYouBug(2)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-03-06 17:01 ÎÒÒªÆÀÂÛ( )

ActionFilterAttributeÀà¼ÈʵÏÖÁËIactionFilter½Ó¿Ú£¬Ò²ÊµÏÖIResultFilter½Ó¿Ú¡£ÕâÊÇÒ»¸ö³éÏóÀ࣬ËüÒªÇóÄã±ØÐëÌṩһ¸öʵÏÖ¡£AuthorizeAttributeºÍHandleErrorAttributeÀ࣬Ôò°üº¬ÁËһЩÓÐÓõÄÌØÐÔ£¬²¢ÇÒ¿ÉÒÔ²»±Ø´´

ActionFilterAttributeÀà¼ÈʵÏÖÁËIactionFilter½Ó¿Ú£¬Ò²ÊµÏÖIResultFilter½Ó¿Ú¡£ÕâÊÇÒ»¸ö³éÏóÀ࣬ËüÒªÇóÄã±ØÐëÌṩһ¸öʵÏÖ¡£AuthorizeAttributeºÍHandleErrorAttributeÀ࣬Ôò°üº¬ÁËһЩÓÐÓõÄÌØÐÔ£¬²¢ÇÒ¿ÉÒÔ²»±Ø´´½¨ÅÉÉúÀà½øÐÐʹÓá£

2.2¡¢¹ýÂËÆ÷µÄÓ¦Óá¢Ó¦Ó÷½Ê½ÒÔ¼°Ö´ÐÐ˳Ðò

Ó¦Ó㺠¹ýÂËÆ÷¿ÉÒÔ±»Ó¦Óõ½¿ØÖÆÆ÷ÉÏÒ²¿ÉÒÔÓõ½Action·½·¨ÉÏ£¬Ó¦Óõ½¿ØÖÆÉÏʱ£¬±íʾËùÓеÄAction·½·¨¶¼ÓÐÁËÕâ¸ö¹ýÂËÆ÷£¬²¢ÇÒ¿ÉÒÔ»ìºÏʹÓ㬻ò¶à´ÎʹÓã¬ÈçÏÂËùʾ£º

Public class DemoController£ºController 3 { [C] 6 Public ActionResult Index() 7 { View(); 10 } 11 }

Ó¦Ó÷½Ê½£ºÌØÐԵķ½Ê½£¬ÈçÉÏÃæ´úÂëËùʾ¡£

ÖÁÓÚÒì³£¹ýÂËÆ÷²»·ÖÏȺó£¬Ö»ÒªÅ׳öÒ쳣ʱ¾Í»áÖ´ÐÐÒì³£¹ýÂËÆ÷¡£Èç¹ûÒªµ÷ÕûÖ´ÐÐ˳Ðò£¬¿ÉÒÔͨ¹ýµ÷ÕûOrder·½·¨Öµ´óСÀ´¿ØÖÆÖ´ÐÐ˳Ðò£¬ÖµÔ½Ð¡£¬Ô½ÏÈÖ´ÐС£ÏÂͼÊÇAction/Result¹ýÂËÆ÷Ó¦ÓõÄÖ´ÐÐ˳Ðòͼ

(1)¡¢ÏàͬÀàÐ͹ýÂËÆ÷Ó¦ÓÃʾÀý£ºÁ½¸ö×Ô¶¨ÒåAction¹ýÂËÆ÷MyFirstFilter£¬MyThreeFilterÓ¦Óõ½Í¬Ò»¸öAction·½·¨IndexÉÏ¡£

Three¿ØÖÆÆ÷´úÂëÈçÏ£º

MyFirstFilter ´úÂëÈçÏ£º

MyThreeFilter´úÂëÈçÏ£º

ÔËÐнá¹ûÈçÏ£º

 

(2)¡¢²»Í¬ÀàÐ͹ýÂËÆ÷Ó¦ÓÃʾÀý£ºÓÐÒ»¸ö×Ô¶¨ÒåAction¹ýÂËÆ÷MyFirstFilter£¬ÓÐÒ»¸ö×Ô¶¨ÒåResult¹ýÂËÆ÷MySecondFilter£¬Ó¦Óõ½Í¬Ò»¸öAction·½·¨IndexÉÏ¡£

Three¿ØÖÆÆ÷´úÂëÈçÏ£º

MyFirstFilter ´úÂëÈçÏ£º

MySecondFilter´úÂëÈçÏ£º

ÔËÐнá¹ûÈçÏ£º

¿´ÍêÉÏÃæµÄ½âÊÍ£¬¿ÉÄÜÄãÏÖÔÚ¶ÔÕâЩ¹ýÂËÆ÷µÄÖ´ÐÐ˳Ðò£¬ÒÔ¼°ÈçºÎ×Ô¶¨Òå¹ýÂËÆ÷»¹²»Ã÷°×£¬²»Òª½ô£¬ÏÂÃæÎÒÃÇ»áÖðÒ»½éÉÜÕ⼸¸ö»ù±¾µÄ¹ýÂËÆ÷µÄʹÓã¬ÒÔ¼°ÈçºÎ×Ô¶¨Òå¹ýÂËÆ÷¡£

2.3¡¢Ê¹ÓÃÊÚȨ¹ýÂËÆ÷

ËùÓÐʵÏÖÁËIAuthorizationFilter½Ó¿ÚµÄ¶¼¿ÉÒÔ³Æ֮ΪÊÚȨ¹ýÂËÆ÷:Æ䶨ÒåÈçÏ£º

IAuthorizationFilter 2 { 3 void OnAuthorization(AuthorizationContext filterContext); 4 }

ÓÉÓÚMVC¿ò¼Üϵͳ×Ô´øµÄAuthorizeAttributeʵÏÖÓÐһЩͻ³öµÄ¹¦ÄÜ£¬¶øÕâÖÖÇ£Éæµ½°²È«µÄ´úÂëÒ»¶¨Òª½÷É÷µÄ±àд£¬ËùÒÔÒ»°ãÎÒÃDz»»áÖ±½ÓʵÏÖÕâ¸ö½Ó¿Ú£¬¶øÊÇÈ¥¼Ì³ÐAuthorizeAttributeÕâ¸öÀ࣬²¢ÖØдÆäAuthorizeCore·½·¨£¬Ç©ÃûΪ£º bool AuthorizeCore(HttpContextBase httpContext) ¶ø´¦ÀíÊÚȨʧ°ÜµÄʱºò£¬¿ÉÒÔÖØдÆäHandleUnauthorizedRequest·½·¨£¬ÆäÇ©ÃûΪ£º void HandleUnauthorizedRequest(AuthorizationContext context) ¡£×¢Ò⣺ÑéÖ¤ÓëÊÚȨÊÇÁ½»ØÊ£¬ÑéÖ¤·¢ÉúÔÚÊÚȨ֮ǰ¡£

ĬÈϵÄÊÚȨ¹ýÂËÆ÷ÒѾ­ÓÐÁËÑéÖ¤µÄ¹¦ÄÜ£¬ÆäÑéÖ¤µÄ»úÀíÊÇÀûÓÃAsp.netƽ̨×Ô´øµÄÑéÖ¤»úÖÆ£¬Èç±íµ¥ÑéÖ¤ºÍWindowsÑéÖ¤¡£³ýÁËÑéÖ¤¹¦ÄÜ£¬Ëü±¾Éí»¹ÓÐÊÚȨµÄ¹¦ÄÜ¡£ÊÚȨ¹ýÂËÆ÷ÊÇËùÓйýÂËÆ÷ÖÐ×îÔçÔËÐеġ£

¾­¹ýRouteµ½´ïÁË¿ØÖÆÆ÷µÄʱºò£¬ÔÚµ÷ÓÃAction֮ǰ£¬MVC¿ò¼Ü»á¼ì²âÔÚÏà¹ØµÄActionÉÏÊÇ·ñÓÐÊÚȨ¹ýÂËÆ÷£¬Èç¹ûÓлáµ÷ÓÃOnAuthorization·½·¨£¬Èç¹û´Ë·½·¨Åú×¼ÁËÇëÇ󣬲Żáµ÷ÓÃÏàÓ¦µÄAction¡£

ʹÓÃÊÚȨ¹ýÂËÆ÷¼¸ÖÖÇé¿öÈçÏ£º

1.Ö±½ÓÔÚActionÉÏ»òÕß¿ØÖÆÆ÷ÉϼÓAuthorize£¬±íʾÆôÓÃÁËÑéÖ¤£¬µ«²»Ç£Éæµ½ÊÚȨ¡£

2.Ìí¼ÓAuthorize(Users=“a,b”)]£¬±íʾÆôÓÃÁËÑéÖ¤£¬²¢ÇÒÒ²ÆôÓÃÁËÊÚȨ£¬Ö»ÓÐa»òÕßbÓû§ÄÜ·ÃÎÊ´Ë¿ØÖÆÆ÷¡£

3.µ±Ìí¼ÓAuthorize£¨Roles=“admin,Member”)]ʱµÄ²½ÖèÈçÏ£º

---ÀûÓÃasp.net×Ô´øµÄ½ÇÉ«ÌṩÕߣ¬»òÕßʵÏÖ×Ô¼ºµÄ½ÇÉ«ÌṩÕߣ¬ÊµÏÖ×Ô¼ºµÄ½ÇÉ«ÌṩÕßʱ£¬Ö»ÐèÒª¼¯³ÉRoleProviderÀàÐÍ£¬²¢ÊµÏÖÆäÖеÄËùÓз½·¨»ò²¿·Ö·½·¨£¬×îºÃʵÏÖËùÓз½·¨¡£

---ÔÚWeb³ÌÐòµÄ¸ùĿ¼µÄWeb.configÎļþÖÐÅäÖýÇÉ«¹ÜÀíÕß¡£

---ÔÚÊʵ±µÄActionÖÐÀûÓÃRolesÀàÐÍÀ´·ÃÎÊ×Ô¼º´´½¨µÄRoleProviderÖеÄÏà¹Ø·½·¨¡£

ʹÓÃÄÚÖõÄÊÚȨ¹ýÂËÆ÷

MVC¿ò¼ÜÄÚÖõÄÊÚȨ¹ýÂËÆ÷AuthorizeAttribute£¬ËüÔÊÐíÎÒÃÇʹÓÃÕâ¸öÀàµÄÁ½¸ö¹«¹²ÊôÐÔÀ´Ö¸¶¨ÊÚȨ²ßÂÔ£¬ÈçÏÂËùʾ£º

UsersºÍRolesÁ½ÕßÊDz¢ÇҵĹØϵ£¬ÀýÈçUsers=“a,b,c”,Roles=“admin”,±íʾÓû§ÊÇa£¬b,c ÆäÖÐÒ»¸ö²¢ÇÒÊÇAdmin½ÇÉ«²ÅÄÜ·ÃÎÊ¡£

´´½¨×Ô¶¨ÒåµÄÊÚȨ¹ýÂËÆ÷

·½Ê½Ò»£ºÖ±½ÓʵÏÖIAuthorizationFilter½Ó¿Ú£¬µ«²»ÍƼöÕâÑù×ö£¬ÒòΪǣÉæµ½°²È«·½ÃæµÄ´úÂë¡£

·½Ê½¶þ£º¼Ì³ÐAuthorizeAttributeÕâ¸öÀ࣬²¢ÖØдÆäAuthorizeCore·½·¨£¬Ç©ÃûΪ£º bool AuthorizeCore(HttpContextBase httpContext)£¬´úÂëÈçÏÂËùʾ£º

¡¡

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

Ïà¹ØÎÄÕÂ
  • ASP.NET Core MVCÉÏ´«¡¢µ¼Èë¡¢µ¼³öÖª¶àÉÙ - Jeffcky

    ASP.NET Core MVCÉÏ´«¡¢µ¼Èë¡¢µ¼³öÖª¶àÉÙ - Jeffcky

    2017-03-02 18:01

  • HTML5±êǩѧϰ±Ê¼Ç - ¾ÍÖ»ÊÇСÜø

    HTML5±êǩѧϰ±Ê¼Ç - ¾ÍÖ»ÊÇСÜø

    2017-02-28 11:00

  • С°×ѧϰH5´ÓÈëÃŵ½·ÅÆú - strengthС±¦

    С°×ѧϰH5´ÓÈëÃŵ½·ÅÆú - strengthС±¦

    2017-02-27 16:01

  • ¿çרҵѧϰ±à³ÌµÄ¿à±ÆÉú»î QWQàÓàÓàÓ£¨µÚÒ»ÖÜ£© - ÌìÀä³Ô±ù¹÷¶ù

    ¿çרҵѧϰ±à³ÌµÄ¿à±ÆÉú»î QWQàÓàÓàÓ£¨µÚÒ»ÖÜ£© - ÌìÀä³Ô±ù¹÷¶ù

    2017-02-27 15:01

ÍøÓѵãÆÀ
Ò