asp.net identity 2.1使用StructureMap注入IAuthenticationManager

wmc*_*nsh 5 structuremap asp.net-mvc-5 asp.net-identity-2

任何人都可以指点我的一些样品或指示如何实现这一目标吗?

tra*_*max 9

我没有使用过StructureMap,但我用Autofac和SimpleInjector完成了这个.

Autofac注册看起来像这样:

builder.Register(c => HttpContext.Current.GetOwinContext().Authentication).As<IAuthenticationManager>();
Run Code Online (Sandbox Code Playgroud)

SimpleInjector中的注册如下所示:

container.RegisterPerWebRequest(() => HttpContext.Current.GetOwinContext().Authentication);
Run Code Online (Sandbox Code Playgroud)

通过查看StructureMap教程我可以猜测注册会是这样的:

ForRequestedType<IAuthenticationManager>()
    .TheDefaultIs(() => HttpContext.Current.GetOwinContext().Authentication)
Run Code Online (Sandbox Code Playgroud)


wmc*_*nsh 8

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

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

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

For<IAuthenticationManager>()
    .Use<MyAuthenticationManager>(
     () => new MyAuthenticationManager(HttpContext.Current.GetOwinContext().Authentication));
Run Code Online (Sandbox Code Playgroud)

谢谢@trailmax