Search code examples
c#.netowinautofac

How to use or resolve a service(or dependency) in the Startup class


When using IdentityServerBearerTokenAuthentication middle-ware, I would like to have access to a service that is registered in my WebApi config. Specifically, I want to call a service to give me the default user id and add it to the Claims in the the request. See my TODO in the example code below in the OnValidateIdentity lambda.

//startup.cs
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
            {
                Authority = ConfigurationManager.AppSettings["Federation.IdentityServerPath"]
            });

            // azure functions will authenticate using Azure AD Tokens
            app.UseWindowsAzureActiveDirectoryBearerAuthentication(
                new WindowsAzureActiveDirectoryBearerAuthenticationOptions
                {
                    Audience = ConfigurationManager.AppSettings["ida:Audience"],
                    Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
                    Provider = new OAuthBearerAuthenticationProvider()
                    {
                        OnValidateIdentity = context =>
                        {

                            //add the account id claim if it's specified in the header
                            var accountIdString = context.Request.Headers["X-AccountId"];
                            if (!string.IsNullOrWhiteSpace(accountIdString) && Guid.TryParse(accountIdString, out var _))
                            {
                                context.Ticket.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, accountIdString));
                            }
                            else
                            {
                                //TODO: Need to pull the system user or admin user and jam that account id into NameIdentifier.
                                var systemDefaultId = "";
                                // How do I get a dependency from HttpContext or context.OwinContext?????
                                context.Ticket.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, systemDefaultId));
                            }

                            context.Ticket.Identity.AddClaim(new Claim("cl_aad_user", "true"));

                            return Task.FromResult<object>(null);
                        }
                    }
                }
            );
        }
    }
}

The application services are registered with the autofac builder pattern. e.g. builder.RegisterType<AccountInfoService>().As<IAccountInfoService>().InstancePerRequest(); in a static method that is called in a Global class (Global.asax).

// Global.asax.cs
...
private void Application_Start(object sender, EventArgs e)
        {
            // ... truncated code               
            // registers the services 
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }

I have tried grabbing a service from the HttpContext, but it always resolves to null. (i.e. var db = (IOutcomesContext)HttpContext.Current.Request.RequestContext.HttpContext.GetService(typeof(IAccountService));) I have also checked this answer, but I am not using that middle ware.

This is how I register the dependencies.

  // WebApiConfig.cs
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            RegisterIoc(config);
        }

        private static void RegisterIoc(HttpConfiguration config)
        {
            ContainerBuilder builder = GetIocContainerBuilder();
            builder.RegisterWebApiFilterProvider(config);

            //all the external dependencies
            builder.RegisterType<InstitutionRequestContext>().As<IInstitutionRequestContext>().InstancePerRequest();

            Autofac.IContainer container = builder.Build();
            GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        }

        internal static ContainerBuilder GetIocContainerBuilder()
        {
            ContainerBuilder builder = new ContainerBuilder();

            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

            // ... truncated service list
            builder.RegisterType<AccountInfoService>().As<IAccountInfoService>().InstancePerRequest();

            return builder;
        }

    }

Thanks for the help.


Solution

  • I ended up solving the above problem by switching the application to use the app.UseAutofacMiddleware(container); middle ware as described in this answer.

    For my particular use case, it only involved a few changes, specifically...

    1. removing the autofac from the Global config file (e.g. Global.asax)
    2. updating my WebApiConfig to return the Autofac.IContainer
    3. adding 3 or so method calls to the Startup.Configure class as described in their documentation