最近的更新出现在下面的链接中,以自动刷新存储帐户的令牌:
https ://docs.microsoft.com/en-us/azure/storage/common/storage-auth-aad-msi
我已经修改了上面的代码,并成功地使用 Azure Data Lake Store Gen1 对其进行了测试,以自动刷新 MSI 令牌。
为了实现 ADLS Gen1 的代码,我需要两个库:
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.2.0-preview3" />
<PackageReference Include="Microsoft.Azure.Storage.Common" Version="10.0.3" />
然后,我使用此代码创建了一个带有不断刷新令牌的 AdlsClient 实例:
var miAuthentication = new AzureManagedIdentityAuthentication("https://datalake.azure.net/");
var tokenCredential = miAuthentication.GetAccessToken();
ServiceClientCredentials serviceClientCredential = new TokenCredentials(tokenCredential.Token);
var dataLakeClient = AdlsClient.CreateClient(clientAccountPath, serviceClientCredential);
下面是我从文章中修改的用于一般刷新令牌的类。通过提供相关资源,这现在可用于自动刷新 ADLS Gen1(“ https://datalake.azure.net/ ”)和存储帐户(“ https://storage.azure.com/ ”)的 MSI 令牌实例化时的地址AzureManagedIdentityAuthentication
。确保使用链接中的代码StorageCredentials
为存储帐户创建对象。
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Azure.Storage.Auth;
namespace SharedCode.Authentication
{
/// <summary>
/// Class AzureManagedIdentityAuthentication.
/// </summary>
public class AzureManagedIdentityAuthentication
{
private string _resource = null;
/// <summary>
/// Initializes a new instance of the <see cref="AzureManagedIdentityAuthentication"/> class.
/// </summary>
/// <param name="resource">The resource.</param>
public AzureManagedIdentityAuthentication(string resource)
{
_resource = resource;
}
/// <summary>
/// Gets the access token.
/// </summary>
/// <returns>TokenCredential.</returns>
public TokenCredential GetAccessToken()
{
// Get the initial access token and the interval at which to refresh it.
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
var tokenAndFrequency = TokenRenewerAsync(azureServiceTokenProvider, CancellationToken.None).GetAwaiter().GetResult();
// Create credentials using the initial token, and connect the callback function
// to renew the token just before it expires
TokenCredential tokenCredential = new TokenCredential(tokenAndFrequency.Token,
TokenRenewerAsync,
azureServiceTokenProvider,
tokenAndFrequency.Frequency.Value);
return tokenCredential;
}
/// <summary>
/// Renew the token
/// </summary>
/// <param name="state">The state.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>System.Threading.Tasks.Task<Microsoft.Azure.Storage.Auth.NewTokenAndFrequency>.</returns>
private async Task<NewTokenAndFrequency> TokenRenewerAsync(Object state, CancellationToken cancellationToken)
{
// Use the same token provider to request a new token.
var authResult = await ((AzureServiceTokenProvider)state).GetAuthenticationResultAsync(_resource);
// Renew the token 5 minutes before it expires.
var next = (authResult.ExpiresOn - DateTimeOffset.UtcNow) - TimeSpan.FromMinutes(5);
if (next.Ticks < 0)
{
next = default(TimeSpan);
}
// Return the new token and the next refresh time.
return new NewTokenAndFrequency(authResult.AccessToken, next);
}
}
}