如何获取Azure存储帐户密钥

时间:2016-12-15 11:39:49

标签: c# azure azure-storage azure-active-directory azure-storage-account

我想使用C#将文件上传到Azure存储帐户,该帐户是使用C#自动生成的(作为Service Fabric资源组的一部分,具有已知名称)。

我需要将文件上传为blob,以便公开发布。

教程Get started with Azure Blob storage using .NET使用存储在App.config文件中的连接字符串。 由于我想使用待生成的存储帐户,我不能使用这样的方法。

首选方法是以某种方式使用用户的AD来获取存储帐户的密钥。

这个链接:Get Storage Account Key显示了如何使用Rest请求获取它,所以我想有一种方法可以使用C#代码来实现它。

在我看来,解决方案是使用StorageManagementClient class,它具有StorageAccounts属性,但我找不到使用AzureAd对其进行身份验证的方法。

我尝试使用AuthenticationContext.AcquireTokenAsync并获取不同资源的令牌,例如:https://management.azure.com/,但在使用令牌时,我收到以下错误:

Microsoft.WindowsAzure.CloudException: AuthenticationFailed: The JWT token does not contain expected audience uri 'https://management.core.windows.net/'.

使用资源https://management.core.windows.net/时,我收到了另一个错误:

Microsoft.WindowsAzure.CloudException: ForbiddenError: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.

我应该使用不同的资源,不同的方法,还是不可能?

1 个答案:

答案 0 :(得分:3)

要使用Storage Service Management REST,我们需要将资源指定为https://management.core.windows.net/而不是https://management.azure.com/。这是使用经典的存储帐户。

https://management.azure.com/Azure REST service的新终结点。如果要处理新存储帐户,则需要使用此资源。以下是使用新Azure REST供您参考的示例:

POST: https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resrouceGroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}/listKeys?api-version=2016-01-01
Authorization: Bearer {token}