繁体   English   中英

C# Azure 文件共享抛出错误“服务器无法验证请求

[英]C# Azure File Share is throwing error "Server failed to authenticate the request

我正在使用以下代码将文件复制到 Azure 文件共享。 它正在抛出错误,有时它会在我这样做时工作

  1. 就像通过替换连接字符串中的新密钥来重置密钥
  2. 通过更改本地机器中的 cst 和 est 时间(并非所有时间都在工作)

我收到错误

Azure.RequestFailedException:服务器无法验证请求。 确保 Authorization header 的值格式正确,包括签名。 RequestId:b6a51c3f-b01a-0037-7774-d19906000000 Time:2022-09-26T06:53: 52.3042347Z Status: 403 (Server failed to authentication the request.确保 Authorization header 的值正确形成,包括签名。) :AuthenticationFailed 附加信息:AuthenticationErrorDetail:在 HTTP 请求“Xrp6MB0=”中找到的 MAC 签名与任何计算的签名不同。 服务器使用以下字符串进行签名:'GET Thu, 22 Sep 2022 10:07:55 GMT "0x8DA9C8251A37348" x-ms-client-request-id:25ff53d8-7a55-414a-954b-ff8f6d05bced x-ms-date:Mon, 2022 年 9 月 26 日 06:53:52 GMT x-ms-return-client-request-id:true x-ms-version:2021-08-06

内容:<xml version="1.0" encoding="utf-8"?>AuthenticationFailedServer 未能对请求进行身份验证。 确保 Authorization header 的值格式正确,包括签名。 RequestId:b6a51c3f- Time:2022-09-26T06:53:52.3042347ZHTTP 请求“Xrp6MB0=”中找到的 MAC 签名与任何计算的签名不同。 服务器使用以下字符串签名:'GET

我正在使用访问密钥 1 访问 Azure 文件共享

有人可以让我知道我在代码中做的错误是什么

using Azure;
    using Azure.Storage.Files.Shares;
    using Microsoft.WindowsAzure.Storage.File;
    using System.Collections.Generic;
    
    var connectionString = "DefaultEndpointsProtocol=https;AccountName=XXXXX;AccountKey=lPI20ZMzVjB9xcPmIJQjjIhFIuWs6JxTcxef7Ri3hRMtE3N1ov81gpabOVe+0BKEFiEZdhcAPhYC+ASt4Yjddw==;EndpointSuffix=core.windows.net";
    var fileShareName = "FileshareName";
    var folderName = "FolderName";
    ShareClient share = new(connectionString, fileShareName);
    if (!share.Exists())
    {
        share.CreateIfNotExists();
    }
    var directory = share.GetDirectoryClient(folderName);
    if(!directory.Exists())
    {
    directory.CreateIfNotExists();    
    }
    var directories = directory.GetFilesAndDirectories();
    
    string sFilename = "sample.pdf";
    string sPath = @"\\abc\Test.pdf";
    
    foreach (var s in directories)
    {
        if (s.Name == "Files")
        {
            var x = directory.GetSubdirectoryClient(s.Name);
            ShareFileClient file = x.GetFileClient(sFilename);
            using (FileStream stream = File.OpenRead(sPath))
            {
                file.Create(stream.Length);
                file.UploadRange(
                    new HttpRange(0, stream.Length),
                    stream);
            }
        }
    }
    Console.ReadLine();

我相信错误的出现是因为运行代码的计算机上的日期/时间设置。

如果您注意到错误消息,则 Azure 存储服务返回的日期/时间是2022-09-26T06:53:52.3042347Z但是请求中发送的日期/时间值是Thu, 22 Sep 2022 10:07:55 GMT

由于运行代码的机器上的当前日期/时间大约比 Azure 存储服务上的日期/时间早 4 天,因此对 Azure 存储的请求将被拒绝。

我建议查看本地计算机上的日期/时间值并确保它是正确的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM