【发布时间】:2014-10-22 02:37:08
【问题描述】:
谁能指点我一些示例的方向或如何实现这一点的说明?
【问题讨论】:
-
此处提供完整代码:stackoverflow.com/a/27244289/351204
标签: 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>();
SimpleInjector 中的注册如下所示:
container.RegisterPerWebRequest(() => HttpContext.Current.GetOwinContext().Authentication);
通过查看 StructureMap 教程,我可以猜到注册会是这样的:
ForRequestedType<IAuthenticationManager>()
.TheDefaultIs(() => HttpContext.Current.GetOwinContext().Authentication)
【讨论】:
OwinContext 之前尝试解析 IAuthenticationManager。通常,当您尝试在没有 http-request 的情况下解决它时会发生这种情况,即在 Global.asax
IAuthenticationManager?
IAuthManager。尝试做后期解决。类似这样的答案:stackoverflow.com/a/6567855/809357
这最初是通过将 Identity 转换为使用 int 作为唯一键值来实现的,如 here 所述。
然后我扩展了它并使用 IAuthenticationManager 创建了一个自定义 AuthenticationManager。
然后我设置 StructureMap 如下:
For<IAuthenticationManager>()
.Use<MyAuthenticationManager>(
() => new MyAuthenticationManager(HttpContext.Current.GetOwinContext().Authentication));
感谢@trailmax
【讨论】: