Home > front end >  ServiceStack - IAuthRepository vs IUserAuthRepository
ServiceStack - IAuthRepository vs IUserAuthRepository

Time:02-18

I’ve to configure my web application to use the ServiceStack built-in ApiKeyAuthProvider. I’ve registered in the container the OrmLiteAuthRepository with the IAuthRepository interface but it throws an exception saying that I’ve not registered the IUserAuthRepository. Could someone explain me the difference? Thanks in advance

EDIT: Sorry, i've made confusion The error is

System.NotSupportedException: 'ApiKeyAuthProvider requires a registered IAuthRepository'

Our AppHost's Configure method is

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

Could you explain me the difference between these two interfaces? we can't figure out (ServiceStack v.6.0.2)

CodePudding user response:

Please refer to the Auth Repository docs for examples of correct usage, e.g:

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

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

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

The IAuthRepository is the minimum interface all Auth Repositories have to implement whilst IUserAuthRepository is the extended interface to enable extended functionality to enabled additional features which all ServiceStack built-in Auth Repositories also implement. But you should never need to register or resolve a IUserAuthRepository, i.e. they should only be registered against the primary IAuthRepository interface.

Resolving Auth Repository

If you need to, the Auth Repository can be accessed from base.AuthRepository or base.AuthRepositoryAsync in your Service where you'll be able to use any IUserAuthRepository APIs since they're all available as extension methods on IAuthRepository, e.g. This example Service calls the IUserAuthRepository.GetUserAuth() method:

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

Whilst here are the recommended APIs to access the Auth Repository outside of your Services:

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