Search code examples
c#.netunity-containerninjectiidentity

Unity equivalent for Ninject's Bind.ToMethod of IPrincipal,IIdentity


I'm trying to replicate the following Ninject syntax in Unity, but not having any luck:

Bind<IIdentity>().ToMethod(c => HttpContext.Current.User.Identity);

I'm thinking it ought to look something like:

IUnityContainer container;
...
container.RegisterType<IIdentity>(HttpContext.Current.User.Identity);

How should it be?


Solution

  • While neontapir's answer could work, that extension method is Obsolete. The correct way to do this now would be to use an InjectionFactory:

    container.RegisterType<IIdentity>(new InjectionFactory(u => HttpContext.Current.User.Identity));