1

I am using Autofac to inject all my project dependencies which is working great. Now I have added a Custom Authorization attribute (I don't need very complex functionality like OWIN and Identity stuff). The custom authorization attribute has dependency to data layer and therefore I am trying to inject it as a property injection. However the property is always Null. The code is below:

 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

    }
}

Code that injects the 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;
    }

Attribute is registered in FilterConfig as filters.Add(new CustomAuthorizationFilterAttribute());

All the wiring up works but AuthorisationHelper is always null.

Any comments will be appreciated.

user1829319
  • 691
  • 1
  • 8
  • 22

2 Answers2

0

Aren't you missing some key registration steps here? Refer to the 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);

EDIT: After being told that the configuration has been setup correctly, have you tried registering your filter like this?

  builder.RegisterType<CustomAuthorizationFilterAttribute>().PropertiesAutowired()
        .AsWebApiAuthorizationFilterFor<QuestionsImportController>()
        .InstancePerRequest();
Ruskin
  • 1,504
  • 13
  • 25
  • I am doing this. As you can see the above function is returning a Container then in I use it as: var container = WebApiAutofacContainer.Container(); config.DependencyResolver = new AutofacWebApiDependencyResolver(container); .... without that nothing will work!!! As I mentioned everything else is working except that property injection!! – user1829319 Aug 02 '15 at 02:42
  • I have edited my answer. I didn't want to assume what you were doing with the container so I wanted to make sure you were registering it all correctly :) – Ruskin Aug 03 '15 at 09:01
  • Hi Ruskin, thanks your reply. I tried your suggestion but still no luck. Property is till always Null. – user1829319 Aug 05 '15 at 11:36
0

seems like this is a known bug in autofac:

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

user1829319
  • 691
  • 1
  • 8
  • 22