Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register HttpContext.User with Autofac

I would like to do the following in Autofac, but not sure how?

This is how you do it in StructureMap

ForRequestedType<IPrincipal>()
  .CacheBy(InstanceScope.Hybrid)
  .TheDefault.Is.ConstructedBy(ctx => HttpContext.Current.User);
like image 495
CRG Avatar asked Mar 05 '11 10:03

CRG


1 Answers

For ASP.NET MVC 1 and 2:

builder.Register(c => HttpContext.Current.User).HttpRequestScoped();

For ASP.NET MVC 3:

builder.Register(c => HttpContext.Current.User).InstancePerHttpRequest();

For Autofac ASP.NET MVC3 integration you may take a look at the documentation (updated link).

For ASP.NET MVC 5:

builder.Register(c => HttpContext.Current.User).InstancePerRequest();
like image 168
Darin Dimitrov Avatar answered Oct 06 '22 15:10

Darin Dimitrov