2014-10-10 88 views
2

在尝试将Ninject与使用ASP.NET Identity的新Web API 2项目结合使用时,我遇到了一些奇怪的现象。我无法获得传递给CreatePerOwinContext()的回调以请求Web API控制器。对于MVC控制器,它们工作正常。Ninject OWIN扩展为Web API打破CreatePerOwinContext

步骤重新创建:

  1. 文件 - >新建VS 2013 ASP.NET Web应用程序
  2. 检查MVC &的WebAPI
  3. 在身份验证选择个人
  4. 添加的NuGet包Ninject,Ninject。 Web.Common.OwinHost,Ninject.Web.WebApi.OwinHost &相关软件包
  5. 按照guidance配置ninject for你的Owin支持的应用程序
  6. 在你的ApplicationUserManager.Create()中放置一个断点,然后F5调试
  7. 看到一个请求到你的主控制器工作正常 - 断点被击中。做一个小提琴手请求到API控制器 - 比如,API /帐号/注册和回调根本就不曾叫

我不是一个ninject爱好者,所以我不能确定,如果这件事情我做错了或Ninject OWIN扩展中的错误。我倾向于留下OwinContext,并简单地使用Ninject perHttpRequest范围,但我不确定ASP.NET Identity系统是否会中断。我听说可能会对OwinContext.Get()进行内部调用,如果我没有将事情保留在OWIN上下文中,就会中断。

非常感谢任何人的想法。

回答

3

我最终通过使用Ninject来实现所有终身管理。很明显,.CreatePerOwinContext()是一个stop gap solution,如果使用完整的DI工具,则不需要。

我所看到的唯一一件事就是基于cookie的身份验证,ASP.NET Identity将在OwinContext中查找其UserManager。我使用基于令牌的身份验证,但我已经看到一些人继续前进,并将一个UserManager添加到OwinContext中。

我Ninject绑定:

private static readonly Lazy<IKernel> _lazyKernel = new Lazy<IKernel>(CreateKernel); 

    // Access the singleton kernel via this property 
    public static IKernel Kernel { get { return _lazyKernel.Value; } } 

private void ConfigureNinject(IAppBuilder app) 
{ 
    app.UseNinjectMiddleware(() => Kernel); 
    Kernel.Bind<IAppBuilder>().ToConstant(app); 
} 

private static void RegisterServices(IKernel kernel) 
{ 
    kernel.Bind<AuthDbContext>().ToSelf().InRequestScope().WithConstructorArgument("connectionString", "MyConnectionString"); 
    kernel.Bind<IUserStore<MyApplicationUser, int>>().To<MyUserStore>().InRequestScope(); 
    kernel.Bind<IRoleStore<MyRole, int>>().To<MyRoleStore>().InRequestScope(); 
    kernel.Bind<MyUserManager>().ToSelf().InRequestScope(); 
    kernel.Bind<ISecureDataFormat<AuthenticationTicket>>().To<SecureDataFormat<AuthenticationTicket>>(); 
    kernel.Bind<IDataSerializer<AuthenticationTicket>>().To<TicketSerializer>(); 
    kernel.Bind<IDataProtector>().ToMethod(x => x.Kernel.Get<IAppBuilder>().GetDataProtectionProvider().Create("ASP.NET Identity")); 
    kernel.Bind<ITextEncoder>().To<Base64UrlTextEncoder>(); 
}