How to get IAuthenticationManager implementation displayed in StructureMap

I am struggling to use StructureMap and Microsoft AspNet Identity as I am unable to figure out how to implement the IAuthenticationManager implementation. I'm trying to map this explicitly to the DefaultRegistry in the example below.

   public class DefaultRegistry : Registry {

        public DefaultRegistry() {
            Scan(
                scan => {
                    scan.TheCallingAssembly();
                    scan.WithDefaultConventions();
                    scan.With(new ControllerConvention());
                });

            For<IAuthenticationManager>().Use(() => HttpContext.Current.GetOwinContext().Authentication);
        }
   }

      

The main problem is that HttpContext.Current is always null, but I'm not even sure if this will work, even if it's not null. I'm a big newbie with StructureMap and with AspNet Identity, so feel free to call me about something stupid I'm doing here. Thank you for your help!

+3


source to share


2 answers


Well, it looks like the code above works great and I'm not sure why it didn't seem to work before. I don't know what the protocol is to close the problem, but I don't want anyone to think they can't use the above for the link as it works.



0


source


This is really not a StructureMap problem. HttpContext.Current will only be available inside an HTTP request in ASP.Net. If you want to use HttpContext in a StructureMap, I would suggest either making sure you check for null gracefully, or try to switch to using the HttpContextWrapper / HttpContextBase abstraction so you can run this code outside of ASP.Net in your tests.



0


source







All Articles