【问题标题】:AzureStorage: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signatureAzureStorage:服务器未能对请求进行身份验证。确保 Authorization 标头的值正确形成,包括签名
【发布时间】:2021-03-21 04:37:07
【问题描述】:

我几乎浏览了与此论坛和其他论坛相关的所有 SO 答案,但我仍然无法弄清楚实际问题。此应用是控制台应用,将部署在 On-Premise 环境中,并将文件上传到 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/S 我已经确认我的本地系统与正确的 UTC 时间同步。

【问题讨论】:

    标签: .net oauth azure-storage azure-blob-storage


    【解决方案1】:

    我尝试了您问题中的代码,它成功上传了 blob。

    注意:

    1. Storage Blob Data Contributor 角色分配给应用程序

    导航到存储帐户 -> 访问控制 (IAM) -> 添加角色分配

    1. 按照此step 授予您注册的应用程序对 Azure 存储的权限。

    【讨论】:

    • 是的,对我来说第二步丢失了。谢谢
    猜你喜欢
    • 2014-03-18
    • 1970-01-01
    • 2021-09-19
    • 2021-07-29
    • 2022-01-27
    • 2020-01-29
    • 2016-04-04
    • 2014-09-19
    • 1970-01-01
    相关资源
    最近更新 更多