gpt4 book ai didi

azure - 如何使用 Microsoft.WindowsAzure.Storage.Table.CloudTableClient 授权托管标识访问 Azure 表存储

转载 作者:行者123 更新时间:2023-12-03 02:19:02 24 4
gpt4 key购买 nike

我使用 Microsoft.WindowsAzure.Storage C# 库通过存储凭据访问我的 Azure Table Storage 帐户,如下所示。

_CloudStorageAccount = new CloudStorageAccount(
new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
azureStorageAccountName, azureStorageAccountKey),
true
);
_CloudTableClient = _CloudStorageAccount.CreateCloudTableClient();

但是,Microsoft 最近表示,现在可以使用托管身份 ( Authorize access to tables using Azure Active Directory (preview) ) 访问 ATS 服务,并且他们在此处分享了以下代码示例,介绍如何使用托管身份创建表:

public static void CreateTable(string accountName, string tableName)
{
// Construct the table endpoint from the arguments.
string tableEndpoint = string.Format("https://{0}.table.core.windows.net/",
accountName);

// Get a token credential and create a service client object for the table.
TableClient tableClient = new TableClient(new Uri(tableEndpoint),
tableName,
new DefaultAzureCredential());

try
{
// Create the table.
tableClient.Create();

}
catch (RequestFailedException e)
{
Console.WriteLine("Exception: {0}", e.Message);
}
}

这很好,但此示例使用 Azure.Data.Tables.TableClient 而不是我当前使用的 Microsoft.WindowsAzure.Storage.Table.CloudTableClient ,那么有什么方法可以使用 CloudTableClient 显式使用托管身份访问 Azure Table Storage 服务?

最佳答案

您可以使用以下代码通过 Microsoft.WindowsAzure.Storage.Table.CloudTableClient 使用托管标识在 Azure 表存储中创建表

public static async Task createTable(string accountName, string tableName)
{
string tableEndpoint = string.Format("https://{0}.table.core.windows.net/",accountName);
var token = await new AzureServiceTokenProvider().GetAccessTokenAsync("https://storage.azure.com/");
var tokenCredential = new TokenCredential(token);
var storageCredentials = new StorageCredentials(tokenCredential);
var tableClient = new CloudTableClient(new Uri(tableEndpoint), storageCredentials);
var table = tableClient.GetTableReference(tableName);
table.CreateIfNotExistsAsync().Wait();
}

关于azure - 如何使用 Microsoft.WindowsAzure.Storage.Table.CloudTableClient 授权托管标识访问 Azure 表存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69772644/

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