3

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

darthal
  • 543
  • 1
  • 3
  • 12

2 Answers2

7

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>();
mythz
  • 141,670
  • 29
  • 246
  • 390
  • 1
    Thanks Mythz.... great API you guys....can't tell how much I have enjoyed working with a framework as clean and well thought out as service-stack... – darthal Apr 12 '13 at 13:19
1

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.

ozczecho
  • 8,649
  • 8
  • 36
  • 42