2012-12-14 49 views
0

我試圖用一個簡單的自定義角色提供和使用的代碼從這裏:正在使用的實現了這個http://code.google.com/p/sfckopanka/source/browse/trunk/App_Code/OdbcRoleProvider.cs?r=45自定義角色提供程序已初始化但未被使用?

http://msdn.microsoft.com/en-us/library/tksy7hd7(v=vs.100).aspx

這是微軟所有的只是簡單的樣板代碼。

當我調試我的應用程序時,我可以看到我的角色提供程序已初始化,但是當我嘗試檢查角色時沒有調用任何方法。

[Authorize(Roles="Customer")] 

User.IsInRole("Customer") 

我把我的角色提供幾個地方突破點,他們只是從來沒有擊中。

僅供參考我使用的是WebAPI,我沒有使用成員資格提供程序,而是通過消息處理程序使用基本身份驗證。

http://www.piotrwalat.net/basic-http-authentication-in-asp-net-web-api-using-message-handlers/

的基本認證是偉大的工作,但我是個不知道這是什麼阻止我的角色提供被調用。

回答

0

回答此問題以防萬一它可以幫助別人。在上面鏈接的Basi認證碼中,有一個PrincipalProvider類。在這個類中創建的GenericPrincipal,這也需要角色的用戶在,所以我不得不添加一行代碼讓我的角色提供給的GenericPrincipal

 public IPrincipal CreatePrincipal(string username, string password) 
      { 

       if (!MyRepo.Authentication.ValidateUser(username, password)) 
       { 
        return null; 
       } 

       var identity = new GenericIdentity(username); 

       //Code to get my roles from my role provider to use when setting principal 
       string[] roles =Roles.Provider.GetRolesForUser(username); 

       IPrincipal principal = new GenericPrincipal(identity,roles); 

       ShopZioRepo.ClearUserCache(ShopZioGlobal.MyCookies.UserID); 
       var user = ShopZioRepo.GetUserByEmail(username); 
       ShopZioGlobal.MyCookies.UserID = user.SalesRepID; 
       ShopZioGlobal.MyCookies.Username = username; 


       return principal; 
      } 

希望這可以幫助別人。

相關問題