本文介绍了C#生成Azure表存储ConnectionString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用azure subscriptionId和azureApiKey为azure表存储生成connectionString.

I want to generate connectionString for azure table storage using azure subscriptionId and azureApiKey.

我找到了以下Microsoft库: https://github. com/Azure/azure-libraries-for-net/tree/master 但无法理解如何在此处生成connectionString.我什至找不到合适的方法.

I found the following Microsoft library: https://github.com/Azure/azure-libraries-for-net/tree/master but can't understand how can I generate connectionString here. I can't even find appropriate method for it.

我需要其他数据来生成ConnectionString吗?

Do I need additional data to generate ConnectionString?

推荐答案

准备工作

1.在Azure AD中注册一个应用程序,并创建用于访问资源的服务主体.有关更多详细信息,请参阅文档.

1.Registry an app in the Azure AD and create service principal for accessing the resource. More detail please refer to the document.

2.准备认证文件,其内容如下所示.这些值可以从步骤1中获得.

2.Prepare the authentication file with the content as following format. The values can be got from the step 1.

subscription=########-####-####-####-############
client=########-####-####-####-############
key=XXXXXXXXXXXXXXXX
tenant=########-####-####-####-############
managementURI=https\://management.core.windows.net/
baseURL=https\://management.azure.com/
authURL=https\://login.windows.net/
graphURL=https\://graph.windows.net/

3.安装库 Microsoft.Azure.Management.Fluent Microsoft.Azure.Management.ResourceManager.Fluent 在项目中

3.Install the libraries Microsoft.Azure.Management.Fluent and Microsoft.Azure.Management.ResourceManager.Fluent in the project

演示代码:

var credFile = @"auth file path"; // example: c:\tom\auth.txt
var keyName = "key1";
var azure = Azure
            .Configure()
            .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
            .Authenticate(credentials)
            .WithDefaultSubscription(); 
var storageAccount = azure.StorageAccounts.GetByResourceGroup(resourceGroupName, storageName);
var key = storageAccount.RegenerateKey(keyName);
var connectionString = $"DefaultEndpointsProtocol=http;AccountName={storageAccount.Name};AccountKey={key.FirstOrDefault()?.Value}";        

测试结果:

这篇关于C#生成Azure表存储ConnectionString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 11:56