2016-06-30 34 views
1

我想對某些自動查詢執行身份驗證。ServiceStack自動查詢和[身份驗證]屬性

[Authenticate] 
public class BusinessEntitiesService : QueryDb<DataModel.dbo.BusinessEntity> 
{ 
} 

這是我的問題。上面的類在我的ServiceModel項目中...爲了添加[Authenticate]屬性,我需要添加一個對ServiceStack.dll的引用,我認爲這會引起問題(根據以前的指導僅引用ServiceStack。 ServiceModel中的接口)。我無法將上面的類添加到ServiceInterfaces中,因爲那麼我必須在每個使用客戶端的地方引用它。

我也使用GlobalRequestFilter試過......但似乎與AdminFeature插件偷懶:

private bool IsAProtectedPath(string path) 
    { 
     return !path.StartsWith("/auth") && !path.StartsWith("/autoquery"); 
    } 

     GlobalRequestFilters.Add((httpReq, httpResp, requestDto) => 
     { 
      if(IsAProtectedPath(httpReq.PathInfo)) 
       new AuthenticateAttribute().Execute(httpReq, httpResp, requestDto); 
     }); 

enter image description here

不能確定如何最好地處理這個問題。

回答

2

爲了將[Authenticate]屬性應用到自動查詢服務,你需要創建一個custom AutoQuery implementation和運用你的過濾器屬性,如:

[Authenticate] 
public class MyProtectedAutoQueryServices : Service 
{ 
    public IAutoQueryDb AutoQuery { get; set; } 

    public object Any(QueryBusinessEntity query) => 
     AutoQuery.Execute(query, AutoQuery.CreateQuery(query, Request)); 

    public object Any(QueryBusinessEntity2 query) => 
     AutoQuery.Execute(query, AutoQuery.CreateQuery(query, Request)); 
} 

另一種方法是動態的屬性添加到您的自動查詢請求DTO ,但這些都需要Configure()之前註冊的叫,要麼appHost.Init()之前或在您的APPHOST構造函數,例如:

public class AppHost : AppHostBase 
{ 
    public AppHost() 
    { 
     typeof(QueryBusinessEntity) 
      .AddAttributes(new AuthenticateAttribute()); 
    } 
} 
+0

是IAutoQueryDb應該被注入到服務?我得到一個空例外....使用基本上相同的例子,從你提供的鏈接。 –

+0

@ChrisKlepeis是啊'當您註冊AutoQueryFeature時,IAutoQueryDb'在IOC中註冊,例如'Plugins.Add(new AutoQueryFeature()); – mythz

相關問題