问题
I want to upload a file to an Azure Storage account that is automatically generated (As part of a Service Fabric resource group, with a known name), using C#.
I need to upload the file as a blob to allow it to be publicly available.
The tutorial Get started with Azure Blob storage using .NET uses a connection string stored in the App.config file. Since I want to use the to-be-generated storage account, I can't use such a method.
The prefered method is using the user's AD somehow in order to get the key of the storage account.
This link: Get Storage Account Key shows how to GET it with a Rest request, so I guess there is a way to do it using C# code.
It seems to me, that the solution is using the StorageManagementClient class, which has a StorageAccounts property, though I could not find a way to authenticate it using AzureAd.
I tried using AuthenticationContext.AcquireTokenAsync, and aquiring a token for diffenent resources, for instance: https://management.azure.com/
, but when using the token, I get the following error:
Microsoft.WindowsAzure.CloudException: AuthenticationFailed: The JWT token does not contain expected audience uri 'https://management.core.windows.net/'.
When using the resource https://management.core.windows.net/
I get a different error:
Microsoft.WindowsAzure.CloudException: ForbiddenError: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.
Is there a different resource I should use, different method, or maybe it's impossible?
回答1:
To use the Storage Service Management REST, we need to specify the resource to https://management.core.windows.net/
instead of https://management.azure.com/
. And this is using the operate the classic storage account.
The https://management.azure.com/
is the new endpoint for the Azure REST service. If you want to handle the new storage account, you need to use this resource. And below is a sample using the new Azure REST for your reference:
POST: https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resrouceGroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}/listKeys?api-version=2016-01-01
Authorization: Bearer {token}
来源:https://stackoverflow.com/questions/41163279/how-to-get-azure-storage-account-key