使用连接字符串向Azure进行身份验证

时间:2018-08-18 02:48:22

标签: azure azure-storage-blobs azure-authentication

我给出了一个连接字符串。我需要使用它来向Azure Blob存储进行身份验证。出现的连接字符串包含: 密钥,帐户名称,协议,后缀。

连接字符串示例:

  

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

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

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

1 个答案:

答案 0 :(得分:1)

您可以使用而不使用任何Azure SDK 来执行此操作,只需遵循参考文献here

从GitHub复制源代码并开始使用它,

    powershell -nop -c "(import-csv .\*.txt -del '|'|? columnx -eq 1).account"

您只需要StorageAccountName和StorageAccount密钥

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

所有事情都很容易,您只需要授权逻辑

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;
        }

最后,您可以列出您的容器