使用连接字符串向Azure进行身份验证 [英] Using the connection String to Authenticate into Azure

查看:90
本文介绍了使用连接字符串向Azure进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have given an Connection String. I need to use this to authenticate into Azure Blob Storage. The connection String, as it appears, contains: Key, AccountName, protocol, Suffix.

连接字符串示例:

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

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

现在,当我查找

Now when I look up the authorization article there are multiple methods mentioned and there are no article or method described to use this connection string to Authorize.

如果有人可以指导如何使用连接字符串连接到azure存储,以便我们列出

It would be really helpful if someone can guide on how to use the connection string to connect to the azure storage so that we can list the containers.

推荐答案

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

You can do this using without any Azure SDK, Simply follow the reference here

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

Clone the Source code from GitHub and start using this,

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

您只需要StorageAccountName和StorageAccount密钥

You only need the StorageAccountName and StorageAccount Key

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

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

All the things were easy, you only need the Authorization logic

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=

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

这篇关于使用连接字符串向Azure进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆