When the ConfidentialClientApplication.AcquireTokenForClient() will return the same access token?

24 Views Asked by At

My project is using unity container and Microsoft.Identity.Client 4.22 which not support the cache methods. I register one class like AppTokenProvider as the singleton per container, and in the class, I initialize the IConfidentialClientApplication instance like app as one global property in the container. and every time I resolve the AppTokenProvider and use the app.AcquireTokenForClient for single resource, and I wish the AccessToken is the same when I resolve the instance and get token. but seems not, is that because the app property actually be changed, or serialized and deserialized? BTW, the app's hash code is always the same.

there is the pseudo code

using Microsoft.Practices.Unity;
container = new UnityContainer();
ServiceLocator.SetContainer(container);
ServiceLocator.Container.RegisterInstance<IAppTokenProvider >(
    new AppTokenAcquirer(
       clientId,
       authority,
       cert), 
    new ContainerControlledLifetimeManager();


public sealed class AppTokenProvider: IAppTokenProvider  
{
    private IConfidentialClientApplication tokenApplication;

    public AppTokenProvider(
        string clientId,
        string authority,
        X509Certificate2 aadCert)
    {
        IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(clientId)
                                   .WithAuthority(authority)
                                   .WithCertificate(aadCert)
                                   .Build();

        Log($"hash code: {this.tokenApplication.GetHashCode()}.");
    }

    private string GetAADAccessToken(string resourceUri)
    {
        Log($"hash code: {this.tokenApplication.GetHashCode()}.");

        // Acquire application token
        var result  tokenApplication
                        .AcquireTokenForClient(new[] { resourceUri })
                        .WithSendX5C(true)
                        .ExecuteAsync(CancellationToken.None)
                        .ConfigureAwait(false)
                        .GetAwaiter()
                        .GetResult();
        return result.AccessToken;
    }
0

There are 0 best solutions below