AcquireTokenForClient (MSAL) 是否使用令牌缓存?

Does AcquireTokenForClient (MSAL) use the token cache?

我正在(成功地)使用带有 MSAL 的客户端凭据流来验证这样的应用程序:

private static async Task<AuthenticationResult> getAuthResultNonInteractively()
{
    string[] scopes = {"api://xxx/.default"};

    IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.CreateWithApplicationOptions(
            new ConfidentialClientApplicationOptions
            {
                TenantId = "xxx",
                ClientId = "xxx",
                RedirectUri = "http://localhost",
                ClientSecret = "xxx"
            })
        .Build();
    
    // Desired behaviour: acquires token online only if token does not 
    // exist in cache or is expired
    AuthenticationResult authResult = await app.AcquireTokenForClient(scopes)
        .ExecuteAsync();

    return authResult;
}

调用AcquireTokenForClient是否首先尝试在令牌缓存中查找令牌,只有在缓存中不存在令牌或令牌已过期时才在线获取令牌?还是它总是在线获取令牌?如果是后者,我需要更改什么才能获得所需的行为?

AcquireTokenForClient 使用的 AppTokenCache 与您的 IConfidentialClientApplication 的特定实例相关联,因此为了利用令牌缓存,您的 IConfidentialClientApplication 需要长寿(即单身)。