【问题标题】:Asp.Net Web Api and Autofac with Custom Authorisation attribute issue (property injection)具有自定义授权属性问题的 Asp.Net Web Api 和 Autofac(属性注入)
【发布时间】:2015-07-04 01:31:55
【问题描述】:

我正在使用 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 始终为空。

我们将不胜感激。

【问题讨论】:

    标签: asp.net-web-api2 autofac custom-authentication property-injection


    【解决方案1】:

    您是不是错过了一些关键的注册步骤?参考Autofac doco

      // 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();
    

    【讨论】:

    • 我正在这样做。如您所见,上面的函数返回一个容器,然后我将其用作: var container = WebApiAutofacContainer.Container(); config.DependencyResolver = new AutofacWebApiDependencyResolver(container); ....没有那个什么都行不通!!!正如我提到的,除了属性注入之外,其他一切都在工作!!
    • 我已经编辑了我的答案。我不想假设你对容器做了什么,所以我想确保你正确注册它:)
    • 嗨拉斯金,谢谢你的回复。我尝试了你的建议,但仍然没有运气。属性始终为 Null。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      相关资源
      最近更新 更多