我尝试使用 AzureServiceTokenProvider
获取访问 token 来调用自定义服务,但它间歇性地返回过期的 token 。我了解 AzureServiceTokenProvider caches the token and retrieves a new one before expiration ,但这是全新调试 session 中的全新 token 。调试 session 示例:
当前 UTC 时间:2021 年 3 月 6 日 5:57:16 AM
但是返回的token已经过期了?? 世界标准时间 2021 年 3 月 6 日上午 5:27:40
我在本地和部署到 Azure 时都会遇到此行为,但它是间歇性的。
- 截至撰写本文时,我正在使用最新的
Microsoft.Azure.Services.AppAuthentication 1.6.1
。 - 我正在关注 https://learn.microsoft.com/en-us/dotnet/api/overview/azure/service-to-service-authentication#running-the-application-using-managed-identity-or-user-assigned-identity在使用 Visual Studio 进行本地调试时,使用 Azure 中的托管标识获取 token 并使用我的 Azure 帐户(具有自定义服务的 API 权限)。
更新:我还根据https://github.com/Azure/azure-sdk-for-net/issues/16718的建议尝试了将forceRefresh设置为true的GetAuthenticationResultAsync。但仍然获得了相同日期 3/6/2021 5:27:40 AM 的过期 token 。
最佳答案
很难说问题是什么。最好的猜测是获取新 token 的调用失败,因此返回缓存的 token 。
但是,正如您问题中的链接所述,此方法是旧方法,不再推荐。
因此,您可以通过切换到 Azure Identity 客户端库来“解决”该问题 https://learn.microsoft.com/en-us/dotnet/api/overview/azure/identity-readme
关于azure - 为什么 AzureServiceTokenProvider 返回过期的访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66502950/