Repository navigation
Description
Which version of Microsoft Identity Web are you using?
Currently using Microsoft.Identity.Web 1.14.1, because 1.15x versions aren't compatible with Azure Functions on .NET Core 3.1 due to differing versions of System.IdentityModel.Tokens.Jwt (see #1386).
Where is the issue?
- Web app
- Sign-in users
- Sign-in users and call web APIs
- Web API
- Protected web APIs (validating tokens)
- Protected web APIs (validating scopes)
- Protected web APIs call downstream web APIs
- Token cache serialization
- In-memory caches
- Session caches
- Distributed caches
- Other (please describe)
Is this a new or an existing app?
c. This is a new app or an experiment.
Repro
- Create a new Functions app with the
func2template andSingleOrgauth:dotnet new func2 -i Microsoft.Identity.Web.ProjectTemplates::1.14.1 --auth SingleOrg - Add
.EnableTokenAcquisitionToCallDownstreamApi().AddInMemoryTokenCaches()to the auth setup - Configure the
AzureAdsettings in appsettings.json, includingClientId - Add a new timer function that injects
ITokenAcquisitionand callsGetAccessTokenForAppAsync
Full code: https://github.com/Choonster/AzureFunctionsMicrosoftIdentityWebTest
Expected behavior
Function receives access token for the application.
Actual behavior
An exception about a missing ClientId is thrown:
MSAL.NetCore.4.34.0.0.MsalClientException:
ErrorCode: no_client_id
Microsoft.Identity.Client.MsalClientException: No ClientId was specified.
at Microsoft.Identity.Client.AbstractApplicationBuilder`1.Validate()
at Microsoft.Identity.Client.ConfidentialClientApplicationBuilder.Validate()
at Microsoft.Identity.Client.AbstractApplicationBuilder`1.BuildConfiguration()
at Microsoft.Identity.Client.ConfidentialClientApplicationBuilder.BuildConcrete()
at Microsoft.Identity.Client.ConfidentialClientApplicationBuilder.Build()
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 azure_functions_ms_identity_web.TimerFunc.RunAsync(TimerInfo myTimer, ILogger log) in C:\Users\USER\source\repos\azure-functions-ms-identity-web\TimerFunc.cs:line 26
Possible solution
N/A
Additional context / logs / screenshots
The issue seems to be that the MergedOptions.ConfidentialClientApplicationOptions property getter is first called during options binding, which creates the ConfidentialClientApplicationOptions instance and copies the values from the MergedOptions instance before any of the configuration values have been bound. The ConfidentialClientApplicationOptions instance never seems to be updated after that, even by the configureConfidentialClientApplicationOptions action.
In my actual application, I'm trying to acquire a token to call an API from a durable activity function triggered by a timer. The Functions app and the API are both using EasyAuth with separate Azure AD app registrations. I'm configuring the authentication here, and acquiring the token here (called from this function).
Activity
Choonster commentedon Sep 12, 2021
I've managed to work around this for now by injecting
IOptionsMonitor<JwtBearerOptions>and callingjwtBearerOptions.Get(Constants.Bearer), which triggers the merging of the options. I don't know if there's a better way to do this.blakesparkes commentedon Nov 3, 2021
I tried to reproduce your workaround, but I am encountering the following exception. If anyone could shed some light on this problem, I'd appreciate it. Using MIW 1.19.0
Method not found: 'Microsoft.IdentityModel.Tokens.TokenValidationParameters Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerOptions.get_TokenValidationParameters()'. at Microsoft.Identity.Web.MicrosoftIdentityWebApiAuthenticationBuilderExtensions.<>c__DisplayClass3_0.<AddMicrosoftIdentityWebApiImplementation>b__0(JwtBearerOptions options, IServiceProvider serviceProvider, IOptionsMonitor1 mergedOptionsMonitor, IOptionsMonitor1 msIdOptionsMonitor, IOptions1 msIdOptions)at Microsoft.Extensions.Options.ConfigureNamedOptions
5.Configure(String name, TOptions options) at Microsoft.Extensions.Options.OptionsFactory1.Create(String name)at Microsoft.Azure.WebJobs.Hosting.WebJobsOptionsFactory
1.Create(String name) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Hosting\OptionsFormatter\WebJobsOptionsFactory.cs:line 36 at Microsoft.Extensions.Options.OptionsMonitor1.<>c__DisplayClass11_0.b__0()at System.Lazy
1.ViaFactory(LazyThreadSafetyMode mode) at System.Lazy1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)at System.Lazy
1.CreateValue() at System.Lazy1.get_Value()at Microsoft.Extensions.Options.OptionsCache
1.GetOrAdd(String name, Func1 createOptions)at Microsoft.Extensions.Options.OptionsMonitor
1.Get(String name) at Oha.EventHandler.AF.Clients.ApiClient.<HandleEvent>d__5.MoveNext() in C:\src\Repos\AF - EventGrid Event Handler\Source\Oha.EventHandler.AF\Clients\ApiClient.cs:line 39 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Oha.EventHandler.AF.Services.EventService.<HandleEvent>d__3.MoveNext() in C:\src\Repos\AF - EventGrid Event Handler\Source\Oha.EventHandler.AF\Services\EventService.cs:line 28 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Oha.EventHandler.AF.EventHandlerFunction.<Run>d__2.MoveNext() in C:\src\Repos\AF - EventGrid Event Handler\Source\Oha.EventHandler.AF\EventHandlerFunction.cs:line 66tomseida commentedon Nov 12, 2021
I also am encountering this exception and tried the workaround specified by Choonster on Sept 12 comment. I am receiving the same exception blakesparkes. I using MIW 1.20.0. Is there a way to solve this issue?
jennyf19 commentedon Mar 28, 2024
This is fixed on the latest Azure Functions. Or you can use this sample instead. Please reopen if necessary. Thank you.