Register HttpContext.User with Autofac

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

Here's how you do it in StructureMap

ForRequestedType<IPrincipal>()
  .CacheBy(InstanceScope.Hybrid)
  .TheDefault.Is.ConstructedBy(ctx => HttpContext.Current.User);
+3
source share
1 answer

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();

To integrate Autocac ASP.NET MVC3 you can take a look at the documentation (updated link).

For ASP.NET MVC 5:

builder.Register(c => HttpContext.Current.User).InstancePerRequest();
+10
source

Source: https://habr.com/ru/post/1796474/


All Articles