Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Web API 2自托管应用程序中通过Unity注册IAuthenticationManager?_C#_Asp.net Web Api_Asp.net Identity_Owin_Self Hosting - Fatal编程技术网

C# 如何在Web API 2自托管应用程序中通过Unity注册IAuthenticationManager?

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.

我希望我的Web API应用程序通过ASP.NET身份登录用户。 在我的
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实例?