使用Autofac在Web API 2 AccountController中注入ISecureDataFormat [英] Injecting ISecureDataFormat in Web API 2 AccountController using Autofac

查看:150
本文介绍了使用Autofac在Web API 2 AccountController中注入ISecureDataFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我尝试过:

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

并出现错误:

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

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

我遇到的所有问题似乎都没有使用ASP.NET Identity的最新稳定版本来解决.

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

非常感谢您的帮助.

推荐答案

您必须做相反的事情.使用 Autofac ,您可以将一种类型注册为服务.

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

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

并基于此答案,看来您还需要注册IDataSerializer<AuthenticationTicket>IDataProtector实现.

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>(); 

这篇关于使用Autofac在Web API 2 AccountController中注入ISecureDataFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆