我想对某些自动查询强制进行身份验证。
[Authenticate]
public class BusinessEntitiesService : QueryDb<DataModel.dbo.BusinessEntity>
{
}
这是我的问题。上面的类位于我的 ServiceModel 项目中...为了添加 [Authenticate] 属性,我需要添加对 ServiceStack.dll 的引用,我认为这可能会导致问题(根据之前的指导,仅引用 ServiceStack.dll)。 ServiceModel 中的接口(interface))。我无法将上述类添加到 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);
});
不太确定如何最好地处理这个问题。
最佳答案
为了将 [Authenticate]
属性应用于 AutoQuery 服务,您需要创建 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));
}
另一种方法是动态地将属性添加到 AutoQuery Request DTO 中,但这些属性需要在调用 Configure()
之前(或者在 appHost.Init()
之前)注册> 或在您的 AppHost 构造函数中,例如:
public class AppHost : AppHostBase
{
public AppHost()
{
typeof(QueryBusinessEntity)
.AddAttributes(new AuthenticateAttribute());
}
}
关于ServiceStack AutoQuery 和 [Authenticate] 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38130567/