Skip to content

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

Closed

Description

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.

Activity

  1. jennyf19 commented on Sep 28, 2023

    @jennyf19
    Collaborator

    Seems to be a duplicate

  2. emmanuelpare commented on Sep 28, 2023

    @emmanuelpare

    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.

  3. jmprieur commented on Oct 4, 2023

    @jmprieur
    Collaborator

    Emmanuel Pare (@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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Azure FunctionsduplicateThis issue or pull request already existsquestionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions