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

Ves*_*kov 0 asp.net dependency-injection autofac asp.net-web-api2 asp.net-identity-2

我在Web API 2项目中使用ASP.NET Identity 2.2,但我不确定如何连接使用AutofacISecureDataFormat<AuthenticationTicket>依赖.AccountController

我试过这个:

builder.RegisterType<ISecureDataFormat<AuthenticationTicket>>()
       .As<TicketDataFo??rmat>(); 
Run Code Online (Sandbox Code Playgroud)

并得到错误:

类型'Microsoft.Owin.Security.ISecureDataFormat`1 [Microsoft.Owin.Security.Authenticat ionTicket]'不能分配给服务'Microsoft.Owin.Security.DataHandler.TicketDataFormat'

我遇到的所有问题似乎都没有使用最新的ASP.NET身份稳定版本.

任何帮助是极大的赞赏.

Cyr*_*and 6

你必须做对面.使用Autofac,您可以将类型注册为服务.

builder.RegisterType<TicketDataFo??rmat>()
       .As<ISecureDataFormat<AuthenticationTicket>>(); 
Run Code Online (Sandbox Code Playgroud)

基于这个答案,您似乎还需要注册一个IDataSerializer<AuthenticationTicket>和一个IDataProtector实现.

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