Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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# 如何在ASP.NET MVC 5中将IAuthenticationManager与Ninject绑定?_C#_Asp.net Mvc_Ninject_Asp.net Mvc 5_Owin - Fatal编程技术网

C# 如何在ASP.NET MVC 5中将IAuthenticationManager与Ninject绑定?

C# 如何在ASP.NET MVC 5中将IAuthenticationManager与Ninject绑定?,c#,asp.net-mvc,ninject,asp.net-mvc-5,owin,C#,Asp.net Mvc,Ninject,Asp.net Mvc 5,Owin,我正在尝试使用Ninject绑定iaauthenticationmanager,以便将其注入我的AuthenticationService。问题是我当前从我的控制器上的HttpContext.GetOwinContext()获取iaAuthenticationManager,如下所示: private IAuthenticationManager AuthenticationManager { get { return this.HttpContext.GetOwinCo

我正在尝试使用Ninject绑定
iaauthenticationmanager
,以便将其注入我的
AuthenticationService
。问题是我当前从我的
控制器上的
HttpContext.GetOwinContext()
获取
iaAuthenticationManager
,如下所示:

private IAuthenticationManager AuthenticationManager {
    get {
        return this.HttpContext.GetOwinContext().Authentication;
    }
}

如何使用Ninject创建绑定,以便它知道在运行时从
HttpContext.GetOwinContext()
中查找
iaauthenticationmanager
?可能吗?我的问题有意义吗?提前谢谢

所以,我明白了。Ninject提供了对
HttpContext
的直接访问,因此我做了以下工作:

kernel.Bind<IAuthenticationManager>().ToMethod(
    c =>
        HttpContext.Current.GetOwinContext().Authentication).InRequestScope();
现在,我想这是可以避免的,但到目前为止,这对我是有效的。不过我愿意接受建议。我只是从我的MVC项目中添加了一个对它的引用,并让WebActivator负责初始化它,就像使用常规方法一样

我还将Owin拉到了它自己的名为“X.Owin”的项目中,它包含了常用的Owin启动类,我只是将其重命名为
OwinConfiguration

这两个都是我的“域层”的一部分,它还包含一些其他助手项目。列表中另一个值得注意的项目是我的“X.Mappings”,它用于配置自动映射。它还使用WebActivator进行自我初始化,因此我只需在MVC项目中添加对它的引用


因为我已经从MVC项目中提取了很多代码,所以现在它所做的基本上就是路由和视图渲染。其他所有内容都会根据需要传递给帮助程序项目。

GetOwinContext是Microsoft.Owin.Host.Systemweb nuget包中的一个扩展方法。或者,您可以从HttpContext.Current.Items[“owin.Environment”]@Alex:您是否有权从Global.asax访问HttpContext.Current.GetOwinContext()?@meep,我没有,但这可能是因为我将我的Ninject配置分离到一个不同的项目中,只有该项目具有正确的NuGet包和引用才能使其工作。我的MVC项目本身没有关于Owin的任何线索。我很想看看您是如何将ASP.NET标识从MVC中分离出来的。
/// <summary>
/// Cheaty way to force Visual Studio to find all assembly references, even the ones not directly used by the main project.
/// </summary>
internal static class AssemblyReferences {
    internal static readonly Type t1 = typeof(Ninject.Web.Mvc.MvcModule);
}