简体   繁体   English

实现IAuthenticationFilter,工作单元和依赖注入

[英]Implementing IAuthenticationFilter, Unit of Work and Dependency Injection

I have a Web API 2 project that is implementing a custom IAuthenticationFilter such as the following. 我有一个Web API 2项目,该项目正在实现以下自定义IAuthenticationFilter。

My problem is the UnitOfWork is not injected by Unity in the BasicAuthenticator class. 我的问题是UnityOf没有在UnityAuthenticator类中注入UnitOfWork。 As expected Unity successfully injects UnitOfWork in Controllers. 正如预期的那样,Unity已成功将UnitOfWork注入Controller中。

public class BasicAuthenticator : Attribute, IAuthenticationFilter
{
    [Dependency]
    public UnitOfWork UoW { get; set; }

    public bool AllowMultiple { get { return false; } }     

    public Task AuthenticateAsync(HttpAuthenticationContext context,
                                  CancellationToken cancellationToken)
    {
        // ignore missing implementation
        context.Principal = new ClaimsPrincipal(new[] { id });     
        return Task.FromResult(0);
    }

    public Task ChallengeAsync(HttpAuthenticationChallengeContext context,
                            CancellationToken cancellationToken) {}
}

You will need to create a custom IFilterProvider that will perform a BuildUp() on the applicable filters. 您将需要创建一个自定义IFilterProvider ,该IFilterProvider将在适用的过滤器上执行BuildUp()。 The BuildUp operation will inject all dependencies into the filter. BuildUp操作会将所有依赖项注入到过滤器中。

Here is a UnityFilterProvider that does that: 这是一个执行此操作的UnityFilterProvider:

public class UnityFilterProvider : ActionDescriptorFilterProvider, IFilterProvider
{
    private readonly IUnityContainer container;

    public UnityFilterProvider(IUnityContainer container)
    {
        this.container = container;
    }

    public new IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)
    {
        var filters = base.GetFilters(configuration, actionDescriptor);

        foreach (var filter in filters)
        {
            container.BuildUp(filter.Instance.GetType(), filter.Instance);
        }

        return filters;
    }
}

Next at application startup you need to replace the default filter provider with the custom provider above: 接下来,在应用程序启动时,您需要使用上面的自定义提供程序替换默认过滤器提供程序:

var providers = GlobalConfiguration.Configuration.Services.GetFilterProviders().ToList();
GlobalConfiguration.Configuration.Services.Add(typeof(System.Web.Http.Filters.IFilterProvider),
                                                new UnityFilterProvider(container));

var defaultprovider = providers.First(p => p is ActionDescriptorFilterProvider);
GlobalConfiguration.Configuration.Services.Remove(typeof(System.Web.Http.Filters.IFilterProvider), defaultprovider);

I usually use the Unity bootstrapper for ASP.NET Web API so I put the above code in the UnityConfig.cs after RegisterTypes() . 我通常将Unity引导程序用于ASP.NET Web API,因此将上面的代码放在RegisterTypes()之后的UnityConfig.cs中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM