dotnet / dotnet/aspnetcore

Options not resolved properly from WebApplicationFactory configuration

Open
#34,974 5 comments 0 reactions 0 assignees View on GitHub
area-minimal area-mvc feature-mvc-testing
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Describe the bug
When adding configuration files through the `WebApplicationFactory` the options for token acqusition are not set correctly. A null reference exception is thrown when trying to resolve the correct options.

When running the application host and using the health check to make an authenticated call, the options are resolved as expected and a token is acquired.

When running the test application, the exception is thrown.

Using Microsoft.Identity.Web 1.10 the tests work fine, but the changes introduced in 1.11 breaks the test integration.

### To Reproduce

See repo for full example: https://github.com/span/aspnetcoreoptions

Short version:

**Repro**

Startup (With AzureAd config block in appsettings)
```csharp
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(Configuration)
.EnableTokenAcquisitionToCallDownstreamApi()
.AddInMemoryTokenCaches();
```

Message handler (used to fetch access token for a certain scope)
```csharp
protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var accessToken = await tokenAcquisition.GetAccessTokenForAppAsync(this.scope, authenticationScheme: "Bearer");
```

**Expected behavior**
Access token received from IdP configured in AzureAd.

**Actual behavior**
Null reference exception with stack trace:

```
System.NullReferenceException
Object reference not set to an instance of an object.
at Microsoft.Identity.Web.MergedOptions.PrepareAuthorityInstanceForMsal()
at Microsoft.Identity.Web.TokenAcquisition.BuildConfidentialClientApplication(MergedOptions mergedOptions)
at Microsoft.Identity.Web.TokenAcquisition.GetOrBuildConfidentialClientApplication(MergedOptions mergedOptions)
at Microsoft.Identity.Web.TokenAcquisition.GetAuthenticationResultForAppAsync(String scope, String authenticationScheme, String tenant, TokenAcquisitionOptions tokenAcquisitionOptions)
at Microsoft.Identity.Web.TokenAcquisition.GetAccessTokenForAppAsync(String scope, String authenticationScheme, String tenant, TokenAcquisitionOptions tokenAcquisitionOptions)
at Promotions.Host.HealthChecks.BearerHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) in /Users/daniel/mio/repos/Mio.Services/Mio.Promotions.API/src/Promotions.Host/HealthChecks/BearerHttpMessageHandler.cs:line 28
at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)

### Further technical details
.NET SDK (reflecting any global.json):
Version: 5.0.103
Commit: 72dec52dbd

Runtime Environment:
OS Name: Mac OS X
OS Version: 11.0
OS Platform: Darwin
RID: osx.11.0-x64
Base Path: /usr/local/share/dotnet/sdk/5.0.103/

Host (useful for support):
Version: 5.0.3
Commit: c636bbdc8a

.NET SDKs installed:
3.1.403 [/usr/local/share/dotnet/sdk]
5.0.101 [/usr/local/share/dotnet/sdk]
5.0.103 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.9 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.9 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
https://aka.ms/dotnet-download

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.