Question

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:

  • I was hoping I could register the AdManager using service-stacks built-in IoC "Funq.Container" within my extended "AppHostBase" and access it from within my custom CredentialsAuthProvider? I tried registering it but have not found a way of accessing the IoC (or my registered AdManager object) via the service-stack built in framework.

Am I missing something? Thanks

Was it helpful?

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

OTHER TIPS

Service Stack uses property injection as well. I have used property injection when extending the Service class provided by Service stack.

public class MyService : Service
{
        public MyService(IDb db)  
        {
           //db constructor inject
        }

        public IValidator<MyData> MyDataValidator { get; set; }

        public object Get(MyData request)
        {
           //MyDataValidator is property injected
        }
}

I believe the same logic can be applied to the AuthProvider. But I havent tested it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top