1

私は Autofac を使用して、うまく機能しているすべてのプロジェクトの依存関係を注入しています。これで、カスタム認証属性が追加されました (OWIN や ID などの非常に複雑な機能は必要ありません)。カスタム承認属性はデータ層に依存しているため、プロパティ インジェクションとして挿入しようとしています。ただし、プロパティは常に 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;
    }

属性はfilters.Add(new CustomAuthorizationFilterAttribute());としてFilterConfigに登録されます。

すべての接続は機能しますが、AuthorisationHelper は常に null です。

コメントをお待ちしております。

4

2 に答える 2

0

これは autofac の既知のバグのようです:

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

于 2015-08-05T12:00:16.700 に答える
0

ここでいくつかの重要な登録手順を見逃していませんか? オートファクドコを参照

  // 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 に答える