2017-04-04 90 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