For service to service auth using a bearer token for the app (client id and secret no user context) in .net core using MSAL.NET v4 (nuget Microsoft.Identity.Client v4.3.0) is ConfidentialClientApplication.AcquireTokenForClient().ExecuteAsync()
safe to use in a singleton registered service implemented like this?
public class AADConfidentialClient : IServiceApiAuthorizer
{
private readonly IConfidentialClientApplication _confidentialClient;
public AADConfidentialClient(IOptions<ConfidentialClientApplicationOptions> options)
{
_confidentialClient = ConfidentialClientApplicationBuilder
.CreateWithApplicationOptions(options.Value)
.Build();
}
public async Task<string> GetTokenAsync(IReadOnlyCollection<string> scopes)
{
var result = await _confidentialClient.AcquireTokenForClient(scopes).ExecuteAsync();
return result.AccessToken;
}
}
Registered with the .net core built-in DI as
services.AddSingleton<IServiceApiAuthorizer, AADConfidentialClient>();
I've seen this answer for ADAL.NET https://mcmap.net/q/1468622/-is-acquiretokenasync-thread-safe which mentions working towards thread safety for MSAL v2+ but haven't found anything confirming if this has been done.