ServiceStack - IAuthRepository 与 IUserAuthRepository

ServiceStack - IAuthRepository vs IUserAuthRepository

我必须配置我的 Web 应用程序以使用 ServiceStack 内置的 ApiKeyAuthProvider。我已经在容器中注册了 OrmLiteAuthRepository 和 IAuthRepository 接口,但它抛出一个异常,说我没有注册 IUserAuthRepository。 有人能给我解释一下区别吗? 提前致谢

编辑: 抱歉,我弄糊涂了 错误是

System.NotSupportedException: 'ApiKeyAuthProvider requires a registered IAuthRepository'

我们AppHost的Configure方法是

    public override void Configure(Container container)
    {
        var dbFactory = new OrmLiteConnectionFactory("connString", SqlServerDialect.Provider);
        container.Register<IDbConnectionFactory>(dbFactory);
        container.Register<IUserAuthRepository>(_ => new OrmLiteAuthRepository(dbFactory));
        container.Resolve<IUserAuthRepository>().InitSchema();

        var authProvider = new ApiKeyAuthProvider()
        {
            RequireSecureConnection = false
        };

        Plugins.Add(new AuthFeature(
            () => new AuthUserSession(),
            new IAuthProvider[] {
                authProvider
            }
        ));
    }

你能解释一下这两个接口的区别吗?我们无法弄清楚 (ServiceStack v.6.0.2)

请参考Auth Repository docs的正确用法示例,例如:

container.Register<IDbConnectionFactory>(c =>
    new OrmLiteConnectionFactory(connectionString, SqlServer2012Dialect.Provider));

container.Register<IAuthRepository>(c =>
    new OrmLiteAuthRepository(c.Resolve<IDbConnectionFactory>()));

container.Resolve<IAuthRepository>().InitSchema();

IAuthRepository 是所有 Auth 存储库必须实现的最小接口,而 IUserAuthRepository 是扩展接口,用于启用扩展功能以启用所有 ServiceStack built-in Auth 存储库还具有的附加功能实行。但是你永远不需要注册或解析 IUserAuthRepository,即它们应该只针对主要 IAuthRepository 接口注册。

正在解析 Auth 存储库

如果需要,可以从服务中的 base.AuthRepositorybase.AuthRepositoryAsync 访问 Auth 存储库,您可以在其中使用任何 IUserAuthRepository API,因为它们是IAuthRepository 上的所有扩展方法都可用,例如此示例服务调用 IUserAuthRepository.GetUserAuth() 方法:

public class MyServices : Service
{
    public object Get(MyRequest request) => 
        AuthRepository.GetUserAuth(request.UserId);
}

虽然这里是在您的服务之外访问 Auth Repository 的推荐 API:

var authRepo = HostContext.AppHost.GetAuthRepository();
var authRepoAsync = HostContext.AppHost.GetAuthRepositoryAsync();