ServiceStack AutoQuery 和 [Authenticate] 属性

标签 servicestack servicestack-auth servicestack-autoquery

我想对某些自动查询强制进行身份验证。

[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);
        });

enter image description here

不太确定如何最好地处理这个问题。

最佳答案

为了将 [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/

相关文章:

api - 使用 ServiceStack 的 Swagger 插件,如何实现带有预设值列表的字符串字段

c# - 在 Xamarin.iOS 上使用 ServiceStack.Client

sql-server - 如何在OrmLite中对INT列进行LIKE比较?

servicestack - 更改 DTO 生成中的自动查询返回类型

javascript - ServiceStack AutoQuery 手动发送过滤器

servicestack - 如何防止记录 404 Not Found 异常?

ServiceStack:来自不同设备(电话、网络等)的多次登录 - 我想要每个设备一个 session

ServiceStack.ServiceInterface dll 丢失

servicestack - ServiceStack 中的自定义身份验证提供程序出现 ArgumentNullException