【问题标题】:Using the connection String to Authenticate into Azure使用连接字符串在 Azure 中进行身份验证
【发布时间】:2018-08-18 16:42:13
【问题描述】:

我已经给出了一个连接字符串。我需要使用它对 Azure Blob 存储进行身份验证。看起来,连接字符串包含: 密钥、AccountName、协议、后缀。

连接字符串示例:

DefaultEndpointsProtocol=https;AccountName=$$$$$*****;AccountKey=kjdjkfhdjskhfsdfhlksdhfkldshfishishfldslflkjfklsVvJDynYEkiEqWCZkdfkhdkjshfdshfs==;EndpointSuffix=core.windows.net

现在当我查找authorization article 时,提到了多种方法,并且没有描述使用此连接字符串进行授权的文章或方法。

如果有人可以指导如何使用连接字符串连接到 azure 存储,那将非常有帮助,以便我们可以列出 containers

【问题讨论】:

  • 您是否在使用任何存储 SDK?
  • 否 @GauravMantri,我正在尝试使用 REST API 进行连接。
  • 您是否有任何理由希望直接使用 REST API 而不是使用 SDK? Storage SDK 适用于大多数常用的编程语言,如 .net、java、node、php 等。
  • @GauravMantri:我需要在销售人员中使用它。据我发现没有适用于 Salesforce 的软件包

标签: azure azure-blob-storage azure-authentication


【解决方案1】:

您可以使用无需任何 Azure SDK 来执行此操作,只需按照参考 here

从 GitHub 克隆源代码并开始使用它,

git clone https://github.com/Azure-Samples/storage-dotnet-rest-api-with-auth.git

您只需要 StorageAccountName 和 StorageAccount Key

string StorageAccountName = "myaccount";
string StorageAccountKey = "WoZ0ZnpXzvdAKoCPRrsa7RniqsdsdfedDFddasds+msk4ViI38WUUMS+qZmd7aoxw==";

一切都很简单,你只需要授权逻辑

internal static AuthenticationHeaderValue GetAuthorizationHeader(
           string storageAccountName, string storageAccountKey, DateTime now,
           HttpRequestMessage httpRequestMessage, string ifMatch = "", string md5 = "")
        {
            // This is the raw representation of the message signature.
            HttpMethod method = httpRequestMessage.Method;
            String MessageSignature = String.Format("{0}\n\n\n{1}\n{5}\n\n\n\n{2}\n\n\n\n{3}{4}",
                      method.ToString(),
                      (method == HttpMethod.Get || method == HttpMethod.Head) ? String.Empty
                        : httpRequestMessage.Content.Headers.ContentLength.ToString(),
                      ifMatch,
                      GetCanonicalizedHeaders(httpRequestMessage),
                      GetCanonicalizedResource(httpRequestMessage.RequestUri, storageAccountName),
                      md5);

            // Now turn it into a byte array.
            byte[] SignatureBytes = Encoding.UTF8.GetBytes(MessageSignature);

            // Create the HMACSHA256 version of the storage key.
            HMACSHA256 SHA256 = new HMACSHA256(Convert.FromBase64String(storageAccountKey));

            // Compute the hash of the SignatureBytes and convert it to a base64 string.
            string signature = Convert.ToBase64String(SHA256.ComputeHash(SignatureBytes));

            // This is the actual header that will be added to the list of request headers.
            // You can stop the code here and look at the value of 'authHV' before it is returned.
            AuthenticationHeaderValue authHV = new AuthenticationHeaderValue("SharedKey",
                storageAccountName + ":" + Convert.ToBase64String(SHA256.ComputeHash(SignatureBytes)));
            return authHV;
        }

这是产生哈希授权标头的关键部分,如

Authorization: SharedKey myaccount:38Uh5PAe29Kk8dKZ/km90u2sIyEfKiG5RWCb77VoPpE=

终于可以列出你的容器了

【讨论】:

    猜你喜欢
    • 2012-12-25
    • 2019-08-08
    • 1970-01-01
    • 2012-08-29
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多