Search code examples
ninjectowinninject.web.mvcowin-middlewareninject.web

Ninject Alter Binding base on Owin.Context.User.Identy


I need to change ninject binding base on User.Identity.

I have this scenario: base on user Actor claim which I use for my own purpose. I have to inject on my class constructor the value of Claims.Actor, how can I do that?

public class C { 
    public C (string ActorValue) {
        // code here
    }
}

thanks


Solution

  • This is fairly easy, if i understood the requirement correctly:

    kernel.Bind<C>().ToMethod(
                ctx =>
                  {
                    // you can also do anything like HttpContext.Current.GetOwinContext() etc..
                    var id = HttpContext.Current?.User?.Identity?.Name ?? "not found";
                    return new C(id);
                  });