【问题标题】:Does AcquireTokenForClient (MSAL) use the token cache?AcquireTokenForClient (MSAL) 是否使用令牌缓存?
【发布时间】:2021-04-29 09:27:24
【问题描述】:

我正在(成功地)使用带有 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的调用是否首先尝试在令牌缓存中查找令牌,并且仅当缓存中不存在令牌或令牌已过期时才在线获取它?还是它总是在线获取令牌?如果后者为真,我需要进行哪些更改才能获得所需的行为?

【问题讨论】:

    标签: c# msal


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 2019-06-19
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多