Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net Mvc: Ninject - IPrincipal

I was wondering how I could bind the IPrincipal to HttpContext.Current.User in Asp.net Mvc with Ninject.

Friendly greetings,

Pickels

Edit:

Not sure if it matters but I use my own CustomPrincipal class.

like image 212
Pickels Avatar asked Apr 28 '10 15:04

Pickels


1 Answers

You can do this without the need for a provider in your NinjectModule:

Bind<IPrincipal>()
  .ToMethod(ctx => HttpContext.Current.User)
  .InRequestScope();

Note, I included .InRequestScope() to ensure that the value of the method is cached once per HTTP request. I'd recommend doing so even if you use the provider mechanism.

like image 62
Peter Meyer Avatar answered Oct 13 '22 11:10

Peter Meyer