Search code examples
azureasp.net-web-apiazure-active-directoryautofacautofac-configuration

Setting up AAD Authentication in a Project with Autofac


I have a web API that has AAD auth (in code because it runs in IaaS not PaaS) it works well, but if I add Autofac configuration to the Startup.cs, the Authentication breaks, (if I put Autofac after Auth inizialization Autofac breaks) which makes me think that the configurations are overwriting eachother.

I have tried to find any documentation on how to use both of them together but I have not been able to find any information. One Uses HttpConfiguration and the other uses the IAppBuilder and I don't know how to combine them for them to work together.

here is my Authentication code:

public void ConfigureAuth(IAppBuilder app)
{
 app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
    app.UseCookieAuthentication(new CookieAuthenticationOptions());
    app.Map("/api", inner =>
    {
        inner.UseWindowsAzureActiveDirectoryBearerAuthentication(new WindowsAzureActiveDirectoryBearerAuthenticationOptions()
        {
            Tenant = tenant,
            TokenValidationParameters = new Tokens.TokenValidationParameters
            {
                ValidAudience = Audience
            }
        });
    });
}

and here is the Autofac Code

public static void Register(HttpConfiguration configuration)
{
   var builder = new ContainerBuilder();
   Bootstrapper.Configure(builder);
   var container = builder.Build();
   configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}

what are the best practices for using these two tools together?


Solution

  • I was not properly setting all the WebAPI autofac references to get all the dependencies I followed this Quick start and then Added my references. Bellow is the new ConfigureAutofac function (the configure auth stayed the same)

    private void ConfigureAutofac(IAppBuilder app)
    {
        //Autofac info from https://autofaccn.readthedocs.io/en/latest/integration/webapi.html#quick-start
        var builder = new ContainerBuilder();
    
        // STANDARD WEB API SETUP:
        // Get your HttpConfiguration. In OWIN, you'll create one
        // rather than using GlobalConfiguration.
        var config = new HttpConfiguration();
    
        // Register your Web API controllers.
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); //Register WebApi Controllers
        builder.RegisterType<AutofacManager>().As<IAutofacManager>();
        builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
    
        var container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver((IContainer)container); //Set the WebApi DependencyResolver
    
        // and finally the standard Web API middleware.         
        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(config);
        app.UseWebApi(config);
    }