C# .Net Core using MSTest to test a downstream API call and getting 401 unauthorized

Viewed 36

I have a .net core 6 application that calls a downstream API. The code works correctly when running it in the application and correctly returns data when the endpoint is called from the Swagger UI but when I call it from a MSTest method I am getting a 401.

The "start up" configuration is identical in the test project to the startup in the application. Below is an example:

public static ServiceProvider CreateConfigurationForTest()
        {
            IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddAzureAppConfiguration(options =>
                options
                    .Connect(Environment.GetEnvironmentVariable("AzureAppConfig"))
                    // Load global settings
                    .Select("Global.*", LabelFilter.Null)
                    // Load configuration values with no label
                    .Select(AzureConfigApplicationName + ".*", LabelFilter.Null)
                    // Override with any configuration values specific to current hosting env
                    .Select("Global.*", ConfigurationLabel)
                    .Select(AzureConfigApplicationName + ".*", ConfigurationLabel)

            );


            configurationBuilder.SetBasePath(Directory.GetCurrentDirectory());
            configurationBuilder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            var configuration = configurationBuilder.Build();

            IServiceCollection services = new ServiceCollection();
            

            services.AddMicrosoftIdentityWebApiAuthentication(configuration, "AzureAd")
                .EnableTokenAcquisitionToCallDownstreamApi()
                .AddDownstreamWebApi(Constants.CherwellDataAPI, options =>
                {
                    options.BaseUrl = configuration["Global.ApiUrl.Cherwell"];
                    options.Scopes = "api://8f4486da-f763-4a2c-8ce9-dba4d1fa04d1/.default";
                })
                .AddInMemoryTokenCaches();

            services.AddControllers();



                .AddSingleton<IConfiguration>(configuration)
                .AddScoped<IFacilityHelper, FacilityHelper>()
                

            var serviceProvider = services.BuildServiceProvider();
            return serviceProvider;
}
0 Answers
Related