![](/img/trans.png)
[英]Server was unable to process request. ---> The request failed with HTTP status 401: Unauthorized
[英]AzureStorage: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature
我幾乎瀏覽了與此論壇和其他論壇相關的所有 SO 答案,但我仍然無法弄清楚實際問題。 此應用程序是控制台應用程序,將部署在本地環境中,並將文件上傳到 Azure blob。
我正在使用帶有以下代碼的 WindowsAzure.Storage 9.3.3。
static void CreateBlob(string accountName, string containerName, string blobName, string accessToken)
{
var tokenCredential = new TokenCredential(accessToken);
var storageCredentials = new StorageCredentials(tokenCredential);
var storageAccount = new CloudStorageAccount(storageCredentials, accountName, string.Empty, useHttps: true);
var cloudBlobClient = storageAccount.CreateCloudBlobClient();
var cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);
CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(blobName);
string blobContents = "Blob created by Azure AD authenticated user.";
byte[] byteArray = Encoding.ASCII.GetBytes(blobContents);
try
{
using (MemoryStream stream = new MemoryStream(byteArray))
{
blockBlob.UploadFromStreamAsync(stream).Wait();
}
Console.WriteLine("upload successful");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
並獲得如下所示的令牌。
public static string GetOAuthToken(string activeDirectoryTenantId, string
activeDirectoryApplicationSecret, string activeDirectoryApplicationId)
{
string resourceId = $"https://storage.azure.com";
var authority = String.Format("https://login.windows.net/" + activeDirectoryTenantId);
var credential = new ClientCredential(activeDirectoryApplicationId, activeDirectoryApplicationSecret);
var context = new AuthenticationContext(authority);
var result = context.AcquireTokenAsync(resourceId, credential).Result;
return result.AccessToken;
}
有人可以建議我可能缺少什么嗎?
P/SI 已經確認我的本地系統與正確的 UTC 時間同步。
我嘗試使用您問題中的代碼,它成功上傳了 blob。
筆記:
Storage Blob Data Contributor
角色分配給應用程序導航到存儲帳戶 -> 訪問控制 (IAM) -> 添加角色分配
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.