首页 > 解决方案 > 使用 Azure AD 连接到 Azure 存储时重用 TokenCredential

问题描述

我有一个使用 Azure AD 连接到 Azure 存储 Blob 的 Web API 应用程序。

这是使用的库:

这是创建 BlobServiceClient 的代码。这是针对需要访问存储帐户 blob 的每个请求执行的。我需要

    var blobClientOptions = new BlobClientOptions();
    blobClientOptions.Diagnostics.IsLoggingEnabled = true;
    blobClientOptions.Diagnostics.IsTelemetryEnabled = true;
    blobClientOptions.Diagnostics.IsDistributedTracingEnabled = false;

    blobClientOptions.Retry.Mode = RetryMode.Exponential;
    blobClientOptions.Retry.MaxRetries = 5;

    var storageAccountUrl = $"https://{_storageAccountName}.blob.core.windows.net";
    _blobServiceClient = new BlobServiceClient(new Uri(storageAccountUrl), new DefaultAzureCredential(), blobClientOptions);

我应该缓存和重用 TokenCredential 吗?它有到期时间吗?使用 Azure AD 连接到 Azure 存储帐户时的最佳做法是什么?

标签: azureazure-active-directoryazure-storageazure-blob-storage

解决方案


凭证本身不缓存令牌。在新的 Azure SDK 中,BlobServiceClient(和其他 API 客户端)使用的 HTTP 管道缓存令牌并在需要时从凭据请求新令牌。您可以使用新的凭证或将其缓存,无论哪种方式都有效。我们使用了单例凭证对象。


推荐阅读