2016-06-30 88 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

相关问题