2012-03-21 79 views
3

在爲ServiceStack的文檔,它說,最好的做法是:ServiceStack授權 - 訪問路徑信息

通常ServiceStack調用IAuthSession的方法布爾調用hasPermission(字符串 權限)。此方法檢查IAuthSession中的列表 列表權限是否包含所需的 權限。

IAuthSession存儲在緩存客戶端中,如上所述您可以在的OnAuthenticated方法中填寫此列表,您在本教程的第一部分中已覆蓋 。

我正在與現有系統集成,並讓我的自定義BasicAuthProvider工作(從基本BasicAuthProvider繼承)。身份驗證工作正常,現在我構建了授權部分。我打算使用上面列出的權限列表,但我需要訪問路由信息以確定用戶是否有權訪問特定資源。我在IAuthServiceBase看到有一個IRequestContext它有絕對的URL,但經歷和解析說出來之前,我想必須有獲得訪問ServiceStack航線結構給我或者服務的類名的方式被請求的,或DTO所請求的服務相關的。

這裏是我的BasicAuthProvider類OnAuthenticated方法:

public override void OnAuthenticated(IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo) 
    { 


     UserSession sess = (UserSession)session; 

     Model.User currentUser = UserRepository.GetUserByUsername(session.UserAuthName); 

     //Fill the IAuthSession with data which you want to retrieve in the app eg: 
     session.FirstName = currentUser.Person.FirstName; 
     session.LastName = currentUser.Person.LastName; 
     session.UserName = currentUser.User1; 
     sess.CurrentUser = currentUser; 
     //Important: You need to save the session! 
     authService.SaveSession(session, TimeSpan.FromDays(1)); 
    } 

在MVC我已經使用了一些原始請求數據之前得到控制器和動作名稱,確定資源授權,但這是我正在使用ServiceStack的第一個項目。

回答

2

您可能會發現它的[RequiredPermission]屬性或甚至implementation它可以幫助你,例如,在RequestFilter中傳遞的第三個參數是Request DTO。

而且由於請求DTO與服務映射1:1,所以您可以確定該請求的目的地是IService<TRequest>(或其子類,例如ServiceBase<T>,RestServiceBase<T>)。 您可以通過編程訪問服務的類型,在FilterAttributeCache完成:

var serviceType = EndpointHost.Metadata.GetServiceTypeByRequest(requestDtoType); 

我不知道確切的情況下/你想支持使用情況,但使用[RequiredPermission][RequiredRole]屬性可能有你需要它默認驗證針對內置USERAUTH表可用角色和權限列表。

在外部,您可以使用/ assignroles和/ unassignroles Web服務(作爲AuthorizationFeature插件的一部分)爲用戶分配角色和權限(默認情況下需要具有Admin角色的用戶)。

欲瞭解更多信息,請參閱Authentication/AuthorizationValidation的文檔頁ServiceStack GitHub project wiki

+0

注意,作爲https://github.com/ServiceStack/ServiceStack/commit/17e59065eb195a43443d970302aa2361d62bb2cb的,這已經被替換 VAR的serviceType = EndpointHost.Metadata。GetServiceTypeByRequest(requestDtoType); 我發現這在http://stackoverflow.com/questions/15317540/how-to-find-service-from-servicestack-requestfilter/15319620#15319620。 – Todd 2013-03-10 16:55:29

+0

是的,thx更新。陳舊文檔的另一個案例不跟上impl :) – mythz 2013-03-10 16:59:14