使用 Autofac 在 Web API 2 AccountController 中注入 ISecureDataFormat

Injecting ISecureDataFormat in Web API 2 AccountController using Autofac

我在 Web API 2 项目中使用 ASP.NET Identity 2.2,但我不确定如何使用 [= 连接 AccountControllerISecureDataFormat<AuthenticationTicket> 依赖项25=]Autofac。

我试过这个:

builder.RegisterType<ISecureDataFormat<AuthenticationTicket>>()
       .As<TicketDataFo‌​rmat>(); 

出现错误:

The type 'Microsoft.Owin.Security.ISecureDataFormat`1[Microsoft.Owin.Security.Authenticat‌​ionTicket]' is not assignable to service 'Microsoft.Owin.Security.DataHandler.TicketDataFormat'

None 我遇到的问题似乎可以使用 ASP.NET Identity 的最新稳定版本。

非常感谢任何帮助。

你必须反其道而行之。使用 Autofac,您可以将类型注册为服务。

builder.RegisterType<TicketDataFo‌​rmat>()
       .As<ISecureDataFormat<AuthenticationTicket>>(); 

并且基于this answer,看来你还需要注册一个IDataSerializer<AuthenticationTicket>和一个IDataProtector实现。

builder.RegisterType<TicketSerializer>()
       .As<IDataSerializer<AuthenticationTicket>>();
builder.Register(c => new DpapiDataProtectionProvider().Create("ASP.NET Identity"))
       .As<IDataProtector>();