2017-04-04 86 views
0

如何使用autofac注册ISecureDataFormat<AuthenticationTicket>使用Autofac注册ISecureDataFormat <AuthenticationTicket>

我试着注册了这种方式:

builder.RegisterType<SecureDataFormat<AuthenticationTicket>>() 
     .As<ISecureDataFormat<AuthenticationTicket>>() 
     .InstancePerLifetimeScope(); 

,但我得到的错误:

An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor. ....

InnerException":{"Message":"An error has occurred.","ExceptionMessage":"None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Microsoft.Owin.Security.DataHandler.SecureDataFormat`1[Microsoft.Owin.Security.AuthenticationTicket]' can be invoked with the available services and parameters

AccountController.cs

public AccountController(ApplicationUserManager _userManager, 
         IAuthenticationManager authenticationManager, 
         ISecureDataFormat<AuthenticationTicket> accessTokenFormat) 
{ 
    this._userManager = _userManager; 
    this._authenticationManager = authenticationManager; 
    this._accessTokenFormat = accessTokenFormat; 
} 

没有ISecureDataFormat<AuthenticationTicket> accessTokenFormat在构造函数中一切正常。

SecureDataFormat

#region 
Assembly Microsoft.Owin.Security, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 
#endregion 

using Microsoft.Owin.Security.DataHandler.Encoder; 
using Microsoft.Owin.Security.DataHandler.Serializer; 
using Microsoft.Owin.Security.DataProtection; 

namespace Microsoft.Owin.Security.DataHandler 
{ 
    public class SecureDataFormat<TData> : ISecureDataFormat<TData> 
    { 
     public SecureDataFormat(IDataSerializer<TData> serializer, IDataProtector protector, ITextEncoder encoder); 

     public string Protect(TData data); 
     public TData Unprotect(string protectedText); 
    } 
} 

AuhenticationTicket

#region 
Assembly Microsoft.Owin.Security, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 
#endregion 

using System.Security.Claims; 

namespace Microsoft.Owin.Security 
{ 
    public class AuthenticationTicket 
    { 

     public AuthenticationTicket(ClaimsIdentity identity, AuthenticationProperties properties); 

     public ClaimsIdentity Identity { get; } 
     public AuthenticationProperties Properties { get; } 
    } 
} 
+0

错误消息解释说,* Autofac *无法创建'SecureDataFormat '你能分享它的构造? –

+0

@CyrilDurand我更新了帖子。这堂课来自owin。 – user1031034

回答

0

错误消息解释说,因为它无法找到可用的服务构造Autofac不能创建的SecureDataFormat<AuthenticationTicket>一个实例。

看来你还没有注册所需的SecureDataFormat<AuthenticationTicket>服务。你可以这样他们注册:

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

它的工作原理,谢谢 – user1031034