servicestackfunq

ServiceStack Access Ioc container within Custom CredentialsAuthProvider


I've extended the CredentialsAuthProvider provided by service-stack to allow me to authenticate against a Active-Directory instance. The AD access logic is encapsulated within a custom class called AdManager (see below) e.g.:

public class AdCredentialsAuthProvider : CredentialsAuthProvider
{
    public override bool TryAuthenticate(IServiceBase authService, 
                                            string userName, 
                                            string password)
    {
        IAdManager manager = new AdManager();
        return manager.Authenticate(userName, password);
    }
    ...

Question:

Am I missing something? Thanks


Solution

  • You can access the IOC from within the AuthProvider with the supplied IServiceBase, e.g:

    var addManager = authService.TryResolve<IAdManager>();
    

    Anywhere else you can always resolve dependencies using the Singleton:

    var addManager = HostContext.TryResolve<IAdManager>();
    

    Otherwise if you know it's in an ASP.NET Web Host you also access it via your AppHost singleton:

    var addManager = AppHostBase.Instance.Resolve<IAdManager>();