AuthorizationFilter WebApi中的Unity DI

时间:2016-10-26 13:25:59

标签: c# asp.net-web-api dependency-injection unity-container

我在这里使用此链接作为参考;

https://unity.codeplex.com/discussions/446780

因此,根据链接,我添加了一个UnityActionFilterProvider类;

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

    public UnityActionFilterProvider(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;
    }
}

然后我添加到我的UnityConfig.cs

            public static void RegisterFilterProviders(IUnityContainer UnityDependencyResolver)
    {
        var providers = GlobalConfiguration.Configuration.Services.GetFilterProviders().ToList();

        GlobalConfiguration.Configuration.Services.Add(
            typeof(IFilterProvider),
            new UnityActionFilterProvider(UnityDependencyResolver));

        var defaultprovider = providers.First(p => p is ActionDescriptorFilterProvider);

        GlobalConfiguration.Configuration.Services.Remove(typeof(IFilterProvider), defaultprovider);
    }

然后我在Startup.cs中调用

   public void Configuration(IAppBuilder app)
    {
        HttpConfiguration = new HttpConfiguration();

        LoggingConfig.RegisterLogger();

        ConfigureOAuth(app);

        var unityContainer = UnityConfig.GetConfiguredContainer();

        HttpConfiguration.DependencyResolver = new UnityDependencyResolver(unityContainer);

        //for DI in the filters
        UnityConfig.RegisterFilterProviders(unityContainer);

        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(HttpConfiguration);
        app.UseCors(CorsOptions.AllowAll);
        app.UseWebApi(HttpConfiguration);
    }

最后,我有以下过滤器(注意:我也尝试过这个作为ActionFilter,看看这是否有所不同)

public class HasPermissionAttribute : AuthorizationFilterAttribute
{
    [Dependency]
    public UserPermissionService UserService { get; set; }


    public override void OnAuthorization(HttpActionContext actionContext)
    {

    }

}

然而,当这个被击中时,UserService为空?关于我在做什么的任何想法都在这里吗?

1 个答案:

答案 0 :(得分:1)

希望有一天这会帮助某人。

基本上,这完全取决于我在OA中使用OAuth。

因此,我只需要通过编辑startup.cs来确保配置是通过过滤器传递的,如下所示;

        HttpConfiguration = new HttpConfiguration();

        LoggingConfig.RegisterLogger();

        ConfigureOAuth(app);

        var unityContainer = UnityConfig.GetConfiguredContainer();

        HttpConfiguration.DependencyResolver = new UnityDependencyResolver(unityContainer);

        //for DI in the filters
        UnityConfig.RegisterFilterProviders(unityContainer, HttpConfiguration);

然后简单地注册了注册;

    public static void RegisterFilterProviders(IUnityContainer UnityDependencyResolver, HttpConfiguration configuration)
    {
        var providers = configuration.Services.GetFilterProviders().ToList();

        configuration.Services.Add(
            typeof(IFilterProvider),
            new UnityActionFilterProvider(UnityDependencyResolver));

        var defaultprovider = providers.First(p => p is ActionDescriptorFilterProvider);

        configuration.Services.Remove(typeof(IFilterProvider), defaultprovider);
    }