Asp.net mvc 5 asp.net identity 2.1使用StructureMap注入IAAuthenticationManager

Asp.net mvc 5 asp.net identity 2.1使用StructureMap注入IAAuthenticationManager,asp.net-mvc-5,structuremap,asp.net-identity-2,Asp.net Mvc 5,Structuremap,Asp.net Identity 2,有人能告诉我一些如何实现这一点的示例或说明吗?我没有使用StructureMap,但我已经使用Autofac和SimpleInjector完成了这项工作 Autofac注册如下所示: builder.Register(c => HttpContext.Current.GetOwinContext().Authentication).As<IAuthenticationManager>(); container.RegisterPerWebRequest(() => Ht

有人能告诉我一些如何实现这一点的示例或说明吗?

我没有使用StructureMap,但我已经使用Autofac和SimpleInjector完成了这项工作

Autofac注册如下所示:

builder.Register(c => HttpContext.Current.GetOwinContext().Authentication).As<IAuthenticationManager>();
container.RegisterPerWebRequest(() => HttpContext.Current.GetOwinContext().Authentication);
ForRequestedType<IAuthenticationManager>()
    .TheDefaultIs(() => HttpContext.Current.GetOwinContext().Authentication)
从StructureMap教程中,我可以猜到注册会是这样的:

builder.Register(c => HttpContext.Current.GetOwinContext().Authentication).As<IAuthenticationManager>();
container.RegisterPerWebRequest(() => HttpContext.Current.GetOwinContext().Authentication);
ForRequestedType<IAuthenticationManager>()
    .TheDefaultIs(() => HttpContext.Current.GetOwinContext().Authentication)
ForRequestedType()
.TheDefaultIs(()=>HttpContext.Current.GetOwinContext().Authentication)

这最初是通过将Identity转换为使用int作为所述的唯一键值来实现的

然后我扩展了它,并使用IAAuthenticationManager创建了一个自定义AuthenticationManager

然后,我按如下方式设置StructureMap:

For<IAuthenticationManager>()
    .Use<MyAuthenticationManager>(
     () => new MyAuthenticationManager(HttpContext.Current.GetOwinContext().Authentication));
() .使用( ()=>新的MyAuthenticationManager(HttpContext.Current.GetOwinContext().Authentication));
感谢@trailmax

这是我所认为的,但它在为请求创建
OwinContext
之前抛出了一个错误,因为HttpContext.Current.GetOwinContext()。身份验证为null。这意味着您正在尝试解析
iaauthenticationmanager
。通常,当您尝试在没有http请求的情况下解决它时,即在
Global.asax中,这就是问题所在,我应该在哪里执行此操作?为什么在请求发生之前需要
IAuthenticationManager
?StructureMap也有可能在注册时创建所有实例,并同时尝试解析
IAuthenticationManager
。试着做晚解析。类似这样的回答:很好的解决方案。为胜利而装潢!完整代码可在此处获得: