C# 如何在Web API 2自托管应用程序中通过Unity注册IAuthenticationManager?
我希望我的Web API应用程序通过ASP.NET身份登录用户。 在我的C# 如何在Web API 2自托管应用程序中通过Unity注册IAuthenticationManager?,c#,asp.net-web-api,asp.net-identity,owin,self-hosting,C#,Asp.net Web Api,Asp.net Identity,Owin,Self Hosting,我希望我的Web API应用程序通过ASP.NET身份登录用户。 在我的AuthController中,我执行以下操作: /…获取索赔等… \u authenticationManager.SignIn(索赔实体) 其中,\u authenticationManager是iaAuthenticationManager 我使用的是Unity,因此,iaAuthenticationManager以以下方式注册: container.RegisterType(新注入工厂(o=>HttpContext.
AuthController
中,我执行以下操作:
/…获取索赔等…
\u authenticationManager.SignIn(索赔实体)
其中,\u authenticationManager
是iaAuthenticationManager
我使用的是Unity,因此,iaAuthenticationManager
以以下方式注册:
container.RegisterType(新注入工厂(o=>HttpContext.Current.GetOwinContext().Authentication))代码>
这适用于托管在IIS中的我的应用程序。但我也有一些在自托管环境上运行的集成测试,问题是IAuthenticationManager
解析失败,因为HttpContext.Current
为空
是否有任何方法可以注册IAuthenticationManager实现并在自托管和IIS中使用它?我还希望能够在单元测试中通过控制器的构造函数注入此依赖项。尝试在unity.config中添加以下行:
container.RegisterType<IAuthenticationManager>(new InjectionFactory(o => HttpContext.Current.GetOwinContext().Authentication));
container.RegisterType(新注入工厂(o=>HttpContext.Current.GetOwinContext().Authentication));
取决于使用IAAuthenticationManager的clinet类的代码。最可能的情况是,将依赖项作为IAManager发送给某个类的构造函数。所以你可以把你的mock注入到构造函数中。是的,我正在使用构造函数注入把IAManager推到我的控制器中。问题是如何解决ASP.NET Identity在自托管环境中提供的真实(而不是模拟)实现。因此,您必须为自托管应用程序使用不同的容器设置,但如何以不同的方式获取IAManager实例,而不是从HttpContext.Current或HttpRequestMessage(仅在控制器实例中可用)获取IAManager实例?