microsoft-identity-web icon indicating copy to clipboard operation
microsoft-identity-web copied to clipboard

TokenAcquirerFactory reffers to wrong location for configuration in Azure function V4

Open cvbc2010 opened this issue 1 year ago • 3 comments

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

  1. Create V4 FunctionApp
  2. Add Microsoft.Identity.Web & Microsoft.Identity.Web.DownstreamApi
  3. Add TokenAcquirerFactory and DownstreamApis
  4. GetDefaultInstance() doesn't load any Configuration from appsettings.json(even after set to copy to output directory)
  5. 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.

cvbc2010 avatar Sep 20 '23 19:09 cvbc2010

Seems to be a duplicate

jennyf19 avatar Sep 28 '23 21:09 jennyf19

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 avatar Sep 28 '23 23:09 emmanuelpare

@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.

jmprieur avatar Oct 04 '23 00:10 jmprieur