1

我正在使用 Autofac 注入我所有的项目依赖项,效果很好。现在我添加了一个自定义授权属性(我不需要像 OWIN 和身份这样的非常复杂的功能)。自定义授权属性依赖于数据层,因此我尝试将其作为属性注入进行注入。但是,该属性始终为 Null。代码如下:

 public class CustomAuthorizationFilterAttribute : AuthorizeAttribute,  IAutofacAuthorizationFilter
{
    public IAuthorisationHelper AuthorisationHelper { get; set; }

    public override void OnAuthorization(HttpActionContext actionContext)
    {
        **... removed for brevity**

            **// TODO: this should be injected by autofac and is always null??**
            if (AuthorisationHelper.IsValidUser(username, password, out roleOfUser))
            {
                var principal =
                    new GenericPrincipal((new GenericIdentity(username)),
                        (new[] { roleOfUser }));

                Thread.CurrentPrincipal = principal;
                return;
            }

        ... removed for brevity

    }
}

注入 AuthorizationHelper 的代码:

public static IContainer Container()
    {
        var builder = new ContainerBuilder();
        var assemblies = new List<Assembly>();

        assemblies.Add(Assembly.Load("Kids.Math.Interfaces"));
        assemblies.Add(Assembly.Load("Kids.Math.Data"));
        assemblies.Add(Assembly.Load("Kids.Math.Business"));
        assemblies.Add(Assembly.Load("Kids.Math.ImportExport"));
        assemblies.Add(Assembly.Load("Kids.Math.Common"));
        assemblies.Add(Assembly.Load("Kids.Math.Api"));

        builder.RegisterAssemblyTypes(assemblies.ToArray()).
            AsImplementedInterfaces();

        builder.RegisterType(typeof(MathContext)).As(typeof (DbContext)).InstancePerRequest();

        // Register web API controllers.
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // TODO: this is not working, also this should be generic to register it for all controllers
        // inject the authorisation filter
        builder.RegisterType<AuthorisationHelper>().As<IAuthorisationHelper>();
        builder.Register(c => new CustomAuthorizationFilterAttribute()).PropertiesAutowired()
            .AsWebApiAuthorizationFilterFor<QuestionsImportController>()
            .InstancePerRequest();

        // Set the dependency resolver to be Autofac.
        var container = builder.Build();
        return container;
    }

属性在 FilterConfig 中注册为 filters.Add(new CustomAuthorizationFilterAttribute());

所有的接线工作,但 AuthorisationHelper 始终为空。

任何意见将不胜感激。

4

2 回答 2

0

似乎这是autofac中的一个已知错误:

https://code.google.com/p/autofac/issues/detail?id=289

于 2015-08-05T12:00:16.700 回答
0

您是否在这里错过了一些关键的注册步骤?参考Autofac 文档

  // OPTIONAL: Register the Autofac filter provider.
  builder.RegisterWebApiFilterProvider(config);

  // Set the dependency resolver to be Autofac.
  var container = builder.Build();
  config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

编辑:在被告知配置已正确设置后,您是否尝试过像这样注册您的过滤器?

  builder.RegisterType<CustomAuthorizationFilterAttribute>().PropertiesAutowired()
        .AsWebApiAuthorizationFilterFor<QuestionsImportController>()
        .InstancePerRequest();
于 2015-08-01T08:20:24.157 回答