如何在StructureMap中映射IAuthenticationManager实现

时间:2014-10-10 17:00:52

标签: asp.net-mvc-5 structuremap asp.net-identity

我一直在努力使用StructureMap和Microsoft AspNet Identity,因为我似乎无法弄清楚如何映射IAuthenticationManager。我试图根据下面的示例在DefaultRegistry中显式映射它。

   public class DefaultRegistry : Registry {

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

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

主要问题是HttpContext.Current总是为null但我甚至不确定即使它不是null也会工作。我是StructureMap和AspNet Identity的新手,所以我可以随时打电话给我,告诉我在这里做的任何傻事。谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

这本身并不是一个StructureMap问题。 HttpContext.Current只能在ASP.Net中的HTTP请求中使用。如果你想在StructureMap中使用HttpContext,我建议要么确保你正常检查null,要么尝试切换到使用HttpContextWrapper / HttpContextBase抽象,这样你就可以在你的ASP.Net之外运行那些代码了。测试

答案 1 :(得分:0)

好吧上面的代码似乎工作正常,我不确定为什么它似乎以前没有工作过。我不知道什么是协议来解决问题,但我不想让任何人认为他们不能使用上述内容作为参考,因为它有效。