免费男女视频_国产系列 视频二区_羞羞视频免费入口网站_久国久产久精永久网页_国产免费观看av_一区二区三区日韩在线观看

ASP.NET MVC 2 Authorize - 自定义Authorize的实现

本文主要是轉載的,通過這個小例子可以輕松自定義你的Authorize權限控制,因為我不是特別了解ASP.NET WebForm的MemberShip的實現機制,也不敢說自定義的性能是否過關,目前使用自定義的比較順手。

 

在ASP.NET MVC2中使用自定義的AuthorizeAttribute繞過內置的Membership/Role機制

感謝 DSO at http://stackoverflow.com/users/38087/DSO

在ASP.NET MVC2中,我們可以使用Authorize Filter限制用戶對內容的訪問,如

  1. [Authorize]  
  2. public class MyController : Controller  
  3. {   
  4.    // ...   
  5. }  
  6. // 或者  
  7. [Authorize(Roles="Admin")]  
  8. public class MyController : Controller  
  9. {   
  10.    // ...   
  11. }  

 

但前提是要用到Membership / Role機制。 我們要不就使用內置的機制,要不就派生出自己的。

不管怎樣,都比較麻煩,其實我們可以繞過這套機制,而且還能使用AuthorizeAttribute。

以下是DSO的看法:


With MVC it is simple to bypass the Membership and Role provider framework altogether. Sometimes it is easier to do this than to implement custom Membership/Role providers, in particular if your authn/authz model doesn't quite fit the mold of those providers.

First, you should realize that you don't need to write everything from scratch, you can use the core Forms authentication API, which can be used independently of the Membership/Role provider framework:

  • FormsAuthentication.SetAuthCookie- Call this after user has been authenticated, specify the user name
  • Request.IsAuthenticated- Returns true if SetAuthCookie was called
  • HttpContext.Current.User.Identity.Name- Returns the user name specified in the call to SetAuthCookie

So here is what you do in MVC to bypass the Membership/Role provider:

  1. Authentication : In your controller, authenticate the user using your custom logic.If successful, callFormsAuthentication.SetAuthCookiewith the user name.

  2. Authorization : Create a custom authorize attribute (deriving from AuthorizeAttribute) . In theAuthorizeCoreoverride, implement your custom authorization logic, taking the user inHttpContext.Current.User.Identity.Nameand the roles defined in the Roles property of the AuthorizeAttribute base class. Note you can also define properties on your custom authorization attribute and use that in your authorization logic. For example you can define a property representing roles as enumerated values specific to your app, instead of using the Roles property which is just a string.

  3. Affix your controllers and actions with your custom authorize attribute, instead of the default Authorize attribute.


我看了感覺很受啟發,但卻不太清楚如何重載AuthorizeAttribute的AuthorizeCore方法。為此我做了個Demo:

1. 使用VS2010建立一個ASP.NET MVC2 Web工程Aut,在Model目錄下新建一個MyAuthAttribute類,如下:

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace AuthTest.Models  
  7. {  
  8.     public class MyAuthAttribute : AuthorizeAttribute  
  9.     {  
  10.         // 只需重載此方法,模擬自定義的角色授權機制  
  11.         protected override bool AuthorizeCore(HttpContextBase httpContext)  
  12.         {  
  13.             string currentRole = GetRole(httpContext.User.Identity.Name);  
  14.             if(Roles.Contains(currentRole ) )  
  15.                 return true;  
  16.             return base.AuthorizeCore(httpContext);  
  17.         }  
  18.   
  19.         // 返回用戶對應的角色, 在實際中, 可以從SQL數據庫中讀取用戶的角色信息  
  20.         private string GetRole(string name)  
  21.         {  
  22.             switch(name)  
  23.             {  
  24.                 case "aaa":  return "User";  
  25.                 case "bbb"return "Admin";  
  26.                 case "ccc"return "God";  
  27.                 defaultreturn "Fool";  
  28.             }  
  29.         }  
  30.     }  
  31.   
  32. }  

 

2. 修改HomeController, 如下

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Security;  
  7. using AuthTest.Models;  
  8. namespace AuthTest.Controllers  
  9. {  
  10.     [HandleError]  
  11.     public class HomeController : Controller  
  12.     {  
  13.         public ActionResult Index()  
  14.         {  
  15.             ViewData["Message"] = "歡迎使用 ASP.NET MVC!";  
  16.             // 模擬用戶成功登錄  
  17.             FormsAuthentication.SetAuthCookie("aaa"false);  
  18.             return View();  
  19.         }  
  20.   
  21.         // 驗證我們自定義的AuthorizeAttribute是否起作用,   
  22.          // 此Action只能由角色為“God”的用戶訪問  
  23.         [MyAuth(Roles="God")]  
  24.         public ActionResult About()  
  25.         {  
  26.             return View();  
  27.         }  
  28.     }  
  29. }  

 

3. 按F5調試,再點擊頁面上的“關于”鏈接,哈哈,知道了吧?

主站蜘蛛池模板: 97人人草| 国产成人在线一区二区 | 中文字幕在线观看二区 | 久久亚洲国产午夜精品理论片 | 日日鲁一鲁视频 | 全免费午夜一级毛片真人 | 欧美一级α | 国产成人小视频在线观看 | 香蕉视频99 | 中国黄色一级生活片 | 日日草夜夜操 | 91精品福利视频 | 少妇一级淫片免费放4p | 性视频久久 | 免费毛片观看 | 精品一区二区久久久久久久网精 | 欧美精品一区二区久久 | 我爱我色成人网 | 国产艳妇av视国产精选av一区 | 视频www | 欧美日韩一区,二区,三区,久久精品 | 久久久资源网 | 久久国产精品区 | 久久亚洲精品久久国产一区二区 | av中文在线观看 | 欧产日产国产精品v | 国产伊人色 | 男女无套免费视频 | 国产麻豆交换夫妇 | 精品国产91久久久久久久 | 97porn| 欧美成人精品欧美一级 | 国产男女爽爽爽爽爽免费视频 | 黄色午夜剧场 | 亚洲综合视频一区 | h视频免费观看 | 日本一区免费看 | 欧美成年性h版影视中文字幕 | 日韩黄色免费观看 | 久久色在线 | 俄罗斯hdxxx 日夜操天天干 |