0

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?

Igal Flegmann
  • 582
  • 1
  • 8
  • 19
  • It doesn't look like there's [a Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve) here. For example, we don't see where you create the `HttpConfiguration` and set it in the `IAppBuilder` ([example here](https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/configuring-aspnet-web-api#configuring-web-api-with-owin-self-hosting)). There's no specific "best practice" around Autofac and AAD auth - it's likely not Autofac-specific so much as just how you'd wire it up with any DI container. – Travis Illig Apr 30 '19 at 13:02

1 Answers1

0

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);
}
Igal Flegmann
  • 582
  • 1
  • 8
  • 19