0

I have this code which generates an OAuth token:

public class MsalAuthenticator
{
    private readonly string _clientScope;
    private readonly string _clientTenantId;
    private readonly IConfidentialClientApplication _app;

    public MsalAuthenticator(string clientId, string clientSecret, string clientScope, string clientTenantId)
    {
        _clientScope = clientScope;
        _clientTenantId = clientTenantId;

        _app = ConfidentialClientApplicationBuilder.Create(clientId)
            .WithClientSecret(clientSecret)
            .Build();
    }

    public async Task<string> GetTokenAsync()
    {
        try
        {
            var task = Task.Run(() => _app.AcquireTokenForClient(scopes: new[] { _clientScope })
                 .WithTenantId(_clientTenantId)
                 .ExecuteAsync());
            task.Wait();
            return task.Result.AccessToken;
        }
        catch (Exception e)
        {
            //_logger.Error("Failed to get MSAL Authenticator token", e);
        }

        return string.Empty;
    }
}

https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-overview

Is there any way the token can be generated only if it is expired?

Does the MSAL library have an endpoint that can be used to determine the token expiry?

Also, what strategy can be used to achieve this? Does the existing working token need to be stored in a database?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83

0 Answers0