Injecting ISecureDataFormat in Web API 2 AccountController using Autofac

房东的猫 提交于 2019-12-11 16:28:45

问题


I am using ASP.NET Identity 2.2 in a Web API 2 project but I am unsure how to wire up the ISecureDataFormat<AuthenticationTicket> dependency of the AccountController using Autofac.

I tried this:

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

and getting error:

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

None of the questions I came across seem to work using the latest stable release of ASP.NET Identity.

Any help is greatly appreciated.


回答1:


You have to do the oposite. With Autofac you register a type as a Service.

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

and based on this answer, it seems that you also need to register a IDataSerializer<AuthenticationTicket> and a IDataProtector implementation.

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


来源:https://stackoverflow.com/questions/33244618/injecting-isecuredataformat-in-web-api-2-accountcontroller-using-autofac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!