您可以在不使用任何 Azure SDK的情况下执行此操作,只需按照此处的参考即可
从 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=
最后你可以列出你的容器