簡體   English   中英

注冊ISecureDataFormat <AuthenticationTicket> 使用Autofac

[英]Register ISecureDataFormat<AuthenticationTicket> using Autofac

如何使用autofac注冊ISecureDataFormat<AuthenticationTicket>

我嘗試以這種方式注冊:

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

但是我得到了錯誤:

嘗試創建“ AccountController”類型的控制器時發生錯誤。 確保控制器具有無參數的公共構造函數。 ....

InnerException“:{” Message“:”發生錯誤。“,” ExceptionMessage“:”找不到類型為'Microsoft.Owin.Security.DataHandler.SecureDataFormat'的'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'的構造函數可以使用可用的服務和參數調用“ 1 [Microsoft.Owin.Security.AuthenticationTicket]”

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; }
    }
}

錯誤消息說明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>(); 

我想跟進@Cyril的回答。 我們有一個服務器場,該服務器場使用機器密鑰進行數據保護,因此,經過很多挫敗之后,我們的注冊看起來像這樣:(我們正在使用Autofac進行注冊,因此在這方面可能看起來有些不同)

builder.RegisterType<Base64UrlTextEncoder>().As<ITextEncoder>().InstancePerLifetimeScope();

builder.RegisterType<TicketSerializer>()
     .As<IDataSerializer<AuthenticationTicket>>()
     .InstancePerLifetimeScope();

builder.Register(c =>
    new MachineKeyDataProtectionProvider().Create(
    typeof(OAuthAuthorizationServerMiddleware).Namespace,
    "Access_Token",
    "v1"))
    .As<IDataProtector>()
    .InstancePerLifetimeScope();

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

MachineKeyDataProtectionProvider是圍繞着一個簡單的包裝MachineKeyDataProtector

public virtual MachineKeyDataProtector Create(params string[] purposes)
{
    return new MachineKeyDataProtector(purposes);
}

public virtual DataProtectionProviderDelegate ToOwinFunction()
{
    return purposes =>
    {
        MachineKeyDataProtector dataProtecter = Create(purposes);
        return new DataProtectionTuple(dataProtecter.Protect, dataProtecter.Unprotect);
    };
}

然后,隨后是MachineKeyDataProtector

internal class MachineKeyDataProtector : IDataProtector
{
    private readonly string[] _purposes;

    public MachineKeyDataProtector(params string[] purposes)
    {
        _purposes = purposes;
    }

    public virtual byte[] Protect(byte[] userData)
    {
        return MachineKey.Protect(userData, _purposes);
    }

    public virtual byte[] Unprotect(byte[] protectedData)
    {
        return MachineKey.Unprotect(protectedData, _purposes);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM