我(成功)使用客户端凭据流与 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
的调用是否首先尝试在 token 缓存中查找 token ,并且仅当 token 在缓存中尚不存在或 token 已过期时才在线获取它?或者它总是在线获取 token ?如果后者是正确的,我需要改变什么才能获得所需的行为?
最佳答案
AcquireTokenForClient 使用的 AppTokenCache
与您的 IConfidentialClientApplication
的特定实例相关联,因此为了利用 token 缓存,您的 IConfidentialClientApplication
需要长期存在(即单例)。
关于c# - AcquireTokenForClient (MSAL) 是否使用 token 缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65887066/