Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TokenAcquirerFactory reffers to wrong location for configuration in Azure function V4 #2471

Closed
cvbc2010 opened this issue Sep 20, 2023 · 3 comments
Labels
Azure Functions duplicate This issue or pull request already exists question Further information is requested

Comments

@cvbc2010
Copy link

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.d__3.MoveNext()
at Microsoft.Identity.Web.DownstreamApi.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 cvbc2010 added the question Further information is requested label Sep 20, 2023
@jennyf19 jennyf19 added Azure Functions duplicate This issue or pull request already exists labels Sep 28, 2023
@jennyf19
Copy link
Collaborator

Seems to be a duplicate

@emmanuelpare
Copy link

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.

@jmprieur
Copy link
Collaborator

jmprieur commented Oct 4, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure Functions duplicate This issue or pull request already exists question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants