【问题标题】:Injecting ISecureDataFormat in Web API 2 AccountController using Autofac使用 Autofac 在 Web API 2 AccountController 中注入 ISecureDataFormat
【发布时间】:2015-10-20 18:53:07
【问题描述】:

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

我试过这个:

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

并得到错误:

类型“Microsoft.Owin.Security.ISecureDataFormat`1[Microsoft.Owin.Security.Authenticat‌​ionTicket]”不可分配给服务“Microsoft.Owin.Security.DataHandler.TicketDataFormat”

我遇到的所有问题似乎都不适用于最新的稳定版本的 ASP.NET Identity。

非常感谢任何帮助。

【问题讨论】:

  • 您能与我们分享您的尝试和错误信息吗?
  • 我试过这个:builder.RegisterType&lt;ISecureDataFormat&lt;AuthenticationTicket&gt;&gt;().As&lt;TicketDataFormat&gt;(); 并得到错误:类型 'Microsoft.Owin.Security.ISecureDataFormat`1[Microsoft.Owin.Security.AuthenticationTicket]' 不能分配给服务 'Microsoft.Owin .Security.DataHandler.TicketDataFormat'。

标签: asp.net dependency-injection asp.net-web-api2 autofac asp.net-identity-2


【解决方案1】:

你必须做相反的事情。使用 Autofac 您可以将类型注册为服务。

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

基于this answer,看来你还需要注册一个IDataSerializer&lt;AuthenticationTicket&gt;和一个IDataProtector实现。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    相关资源
    最近更新 更多