gpt4 book ai didi

c# - 通过 AzureServiceTokenProvider 对 CloudTableClient 进行 Azure 存储身份验证

转载 作者:行者123 更新时间:2023-11-30 18:14:00 24 4
gpt4 key购买 nike

我正在考虑使用 Azure AD 来验证对 Azure 存储帐户的访问。

https://learn.microsoft.com/en-us/azure/active-directory/managed-service-identity/services-support-msi#azure-services-that-support-azure-ad-authentication



using Microsoft.Azure.Services.AppAuthentication; // 1.1.0-preview
using Microsoft.WindowsAzure.Storage; // 9.3.0
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Queue;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Threading.Tasks;

class Program
{
static async Task Main(string[] args)
{
string storageAccountName = "fill_in";

AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();

string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://storage.azure.com/");//, tenantId);
TokenCredential tokenCredential = new TokenCredential(accessToken);

StorageCredentials storageCredentials = new StorageCredentials(tokenCredential);

// blobs access
CloudBlobClient cloudBlobClient = new CloudBlobClient(new StorageUri(new Uri($"https://{storageAccountName}.blob.core.windows.net")), storageCredentials);

ContainerResultSegment containerResultSegment = await cloudBlobClient.ListContainersSegmentedAsync(null);

CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("test" + DateTime.Now.Ticks);

await cloudBlobContainer.CreateIfNotExistsAsync();

// queue access
CloudQueueClient cloudQueueClient = new CloudQueueClient(new StorageUri(new Uri($"https://{storageAccountName}.queue.core.windows.net")), storageCredentials);

QueueResultSegment queueResultSegment = await cloudQueueClient.ListQueuesSegmentedAsync(null);

CloudQueue cloudQueue = cloudQueueClient.GetQueueReference("test" + DateTime.Now.Ticks);

await cloudQueue.CreateIfNotExistsAsync();

// table access
CloudTableClient cloudTableClient = new CloudTableClient(new StorageUri(new Uri($"https://{storageAccountName}.table.core.windows.net")), storageCredentials);

// this http request results in "HTTP/1.1 403 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature."
TableResultSegment tableResultSegment = await cloudTableClient.ListTablesSegmentedAsync(null);

CloudTable cloudTable = cloudTableClient.GetTableReference("test" + DateTime.Now.Ticks);

await cloudTable.CreateIfNotExistsAsync();
}
}

尝试使用表会导致 Microsoft.WindowsAzure.Storage.StorageException:“服务器无法验证请求。确保授权 header 的值格式正确,包括签名。'

在 Portal.azure.com 中,我确实看到了以下角色

  • 存储 Blob 数据 ___(预览)
  • 存储队列数据 ___(预览)

以这种方式使用 Azure 存储表目前超出了范围,还是我遗漏了某些内容?

问候,弗洛里安

最佳答案

AAD 身份验证尚不支持表。只有 Blob 和队列,正如您从可用角色中看到的那样。

关于c# - 通过 AzureServiceTokenProvider 对 CloudTableClient 进行 Azure 存储身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51593931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com