【发布时间】:2019-07-08 15:03:50
【问题描述】:
我有一个非常基本的示例,但我似乎无法开始工作。我使用的用户是订阅所有者,因此应该可以访问所有内容。如果我在尝试实际获取 blob 文本时运行以下命令:
StorageException:服务器未能验证请求。确保 Authorization 标头的值正确形成,包括 签名。
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace testmsistorageaccess
{
class Program
{
public static void Main()
{
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
var tokenAndFrequency = TokenRenewerAsync(azureServiceTokenProvider,
CancellationToken.None).GetAwaiter().GetResult();
TokenCredential tokenCredential = new TokenCredential(tokenAndFrequency.Token,
TokenRenewerAsync,
azureServiceTokenProvider,
tokenAndFrequency.Frequency.Value);
StorageCredentials storageCredentials = new StorageCredentials(tokenCredential);
var storageUri = new Uri("https://mystorageaccount.blob.core.windows.net");
var client = new CloudBlobClient(storageUri, storageCredentials);
var container = client.GetContainerReference("bob");
string content = container.GetBlockBlobReference("bob.xml").DownloadTextAsync().Result;
Console.WriteLine($"Got {content}");
}
private static async Task<NewTokenAndFrequency> TokenRenewerAsync(Object state, CancellationToken cancellationToken)
{
const string StorageResource = "https://storage.azure.com/";
var authResult = await ((AzureServiceTokenProvider)state).GetAuthenticationResultAsync(StorageResource);
var next = (authResult.ExpiresOn - DateTimeOffset.UtcNow) - TimeSpan.FromMinutes(5);
if (next.Ticks < 0)
{
next = default(TimeSpan);
}
return new NewTokenAndFrequency(authResult.AccessToken, next);
}
}
}
不知道我在这里做错了什么,我已经检查了它尝试使用的用户,它看起来正确并且具有正确的 AD 租户 ID:
我看到提到使用 UTCNow 在我的本地计算机上检查时间,并确认它与 GMT 时间是正确的,除了我没有发现其他关于如何调试它的信息。
任何帮助表示赞赏
【问题讨论】:
标签: c# azure azure-storage azure-managed-identity