microsoft-identity-web
microsoft-identity-web copied to clipboard
TokenAcquirerFactory reffers to wrong location for configuration in Azure function V4
Microsoft.Identity.Web Library
Microsoft.Identity.Web.DownstreamApi
Microsoft.Identity.Web version
2.13.4
Web app
Not Applicable
Web API
Not Applicable
Token cache serialization
Distributed caches
Description
I am trying to configure a V4 Azure Function(Servicebus Trigger), which is calling external APIs, And I am trying to use DownstreamApi.
The TokenAcquirerFactory.GetDefaultInstance() is refferring to the wrong location(AppData\Local\AzureFunctionsTools\Releases\4.51.0\cli_x64), instead of output directory. the below code is returning the azure function cli location instead of output directory. And it nether adds environment variables to the Configuration inside TokenAcquirerFactory.
// TokenAcquirerFactory.cs in Microsoft.Identity.Web.TokenAquisition protected virtual string DefineConfiguration(IConfigurationBuilder builder) { Assembly assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly(); return Path.GetDirectoryName(assembly!.Location)!; }
Reproduction steps
- Create V4 FunctionApp
- Add Microsoft.Identity.Web & Microsoft.Identity.Web.DownstreamApi
- Add TokenAcquirerFactory and DownstreamApis
- GetDefaultInstance() doesn't load any Configuration from appsettings.json(even after set to copy to output directory)
- Calling CallApiForAppAsync in DownstreamApi results in null reference exception as provided below
Error message
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.DefaultAuthorizationHeaderProvider.<CreateAuthorizationHeaderForAppAsync>d__3.MoveNext() at Microsoft.Identity.Web.DownstreamApi.<CallApiInternalAsync>d__18.MoveNext()
Id Web logs
No response
Relevant code snippets
public override void Configure(IFunctionsHostBuilder builder)
{
Configuration = builder.GetContext().Configuration;
var tokenAcquirerFactory = TokenAcquirerFactory.GetDefaultInstance();
tokenAcquirerFactory.Services
.AddDownstreamApi("xxxx", Configuration.GetSection("xxxx"))
.AddDownstreamApi("xxxx", Configuration.GetSection("xxxx"))
.AddDistributedTokenCaches();
var dapi = sp.GetRequiredService<IDownstreamApi>();
var data = dapi.CallApiForAppAsync("xxxx", options =>
{
options.HttpMethod = HttpMethod.Get;
options.RelativePath = "api/xxx";
}).Result;
}
Regression
No response
Expected behavior
Should load configuration from the appsettings.json file from output directory.
Seems to be a duplicate
The stack trace look identical compared to issue 2410,,
I will try to build the sample https://github.com/Azure-Samples/active-directory-dotnetcore-daemon-v2/tree/master/2-Call-OwnApi/daemon-console and see if we get the same error.
@emmanuelpare : you mean running as an Azure function? btw, we just fixed #2410. but it's not going to solve the problem with the Azure functions, as the azure functions run on a runtime.