gpt4 book ai didi

azure - 使用连接字符串在 Azure 中进行身份验证

转载 作者:行者123 更新时间:2023-12-03 02:57:14 24 4
gpt4 key购买 nike

我已经给出了一个连接字符串。我需要使用它来对 Azure Blob 存储进行身份验证。连接字符串看起来包含: key 、帐户名称、协议(protocol)、后缀。

连接字符串示例:

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

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

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

最佳答案

您可以使用无需任何 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;
}

这是生成哈希授权 header 的关键部分,如

Authorization: SharedKey myaccount:38Uh5PAe29Kk8dKZ/km90u2sIyEfKiG5RWCb77VoPpE=

最后您可以列出您的容器

关于azure - 使用连接字符串在 Azure 中进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51904813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com