2

I want my Web API app to sign in users via ASP.NET Identity. In my AuthController I do the following:

//...getting claimsIdentity etc...

_authenticationManager.SignIn(claimsIdentity)

where _authenticationManager is an instance of IAuthenticationManager

I'm using Unity so IAuthenticationManager is registered in the following way:

container.RegisterType<IAuthenticationManager>(new InjectionFactory(o => HttpContext.Current.GetOwinContext().Authentication));

And that works for my app that is hosted in IIS. But I also have some integration tests that are run on the self-hosted environment and the problem is that IAuthenticationManager resolution fails because HttpContext.Current is null.

Is there any way to register IAuthenticationManager implementation and use it in both self-hosted and IIS? I do also want to be able to inject this dependency via my controller's constructor in unit tests.

Dmitry Khmara
  • 51
  • 1
  • 3
  • depends on code of your clinet class where you are using IAuthenticationManager. Most likely case when you send you dependencies as IAManager to constructor of some class. So that you can inject you mock to constructor. – Vladimir Shmidt Jul 28 '14 at 19:45
  • Yep, I'm using constructor injection to push IAManager to my controller. The question is how to resolve the real (not the mocked) implementation, provided by ASP.NET Identity in the self-hosted environment. – Dmitry Khmara Jul 28 '14 at 19:50
  • http://stackoverflow.com/questions/11347807/httpselfhostserver-and-httpcontext-current so you have to use different container setup for self-hosting app – Vladimir Shmidt Jul 28 '14 at 19:56
  • But how can I get the IAManager instance in a different way, not from HttpContext.Current or HttpRequestMessage (which is only available in the controller instance)? – Dmitry Khmara Jul 28 '14 at 20:47

1 Answers1

1

Try Adding below line in unity.config :

container.RegisterType<IAuthenticationManager>(new InjectionFactory(o => HttpContext.Current.GetOwinContext().Authentication));
abdulbasit
  • 1,856
  • 15
  • 17