I want to generate connectionString for azure table storage using azure subscriptionId and azureApiKey.
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.
Do I need additional data to generate ConnectionString?
Do I need additional data to generate ConnectionString?
Preparation:
1.Registry an app in the Azure AD and create service principal for accessing the resource. More detail please refer to the document.
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.Install the libraries Microsoft.Azure.Management.Fluent and Microsoft.Azure.Management.ResourceManager.Fluent in the project
Demo Code:
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}";
Test Result:
You don't really need to generate it, you can just go in to the azure portal and just create a storage account any copy the connections string:
But, if you want to generate it, you can just fill this string with your account name and key:
"DefaultEndpointsProtocol=https;AccountName={yourAccountName};AccountKey={yourAccountKey};EndpointSuffix=core.windows.net"
If you want to use the .net library, you can find the example here:
https://github.com/Azure-Samples/storage-dotnet-manage-storage-accounts