c# - HTTP 请求 ' ' 中发现的 MAC 签名与任何计算出的签名不同。服务器使用以下字符串来签名 : 'PUT

标签 c# rest azure azure-storage-files azure-files

我正在尝试通过 REST API 创建文件共享,但收到此错误

AuthenticationFailed Server failed to authenticate the request. Make sure the value of > > > Authorization header is formed correctly including the signature. RequestId:aba10b5f-001a-0026-49cb-51d887000000 Time:2017-10-30T22:04:13.9907093ZThe MAC > signature found in the HTTP request > > > 'U8UvWw4KO0fXAk21p/fuXhfqpDdgK7OZn29r5JQ1x4E=' is not the same as any computed > signature. Server used following string to sign: 'PUT

这是我的代码:

static void CreatFileShare()
    {
        string requestMethod = "PUT";
        string urlPath = strShareName + "?restype=share";
        string urlPathResource = "restype:share";

        String canonicalizedResource = String.Format("/{0}/{2}\n{1}", StorageAccountName, urlPathResource, /*strShareName*/ "MFS2");
        try
        {
            HttpWebRequest request = GetWebRequest(requestMethod, urlPath, canonicalizedResource, "CreateShare");
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                Console.WriteLine("Response status code: " + response.StatusCode + ".  Successfully Create the share \"" + strShareName + "\".");
            }
        }
        catch (WebException ex)
        {
            ThrowWebException(ex);
        }
    }

static HttpWebRequest GetWebRequest(string requestMethod, string urlPath, String canonicalizedResource, string MethodType)
    {
        HttpWebRequest request = null;
        try
        {
            const string type = "file";
            const string msVersion = "2017-04-17";
            String dateInRfc1123Format = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
            String canonicalizedHeaders = "";

            //canonicalizedHeaders = String.Format("x-ms-date:{0}\nx-ms-version:{1}", dateInRfc1123Format, msVersion);
            canonicalizedHeaders = String.Format("x-ms-date:{0}\nx-ms-version:{1}\n", dateInRfc1123Format, msVersion);
            if (MethodType == "CreateFile")
            {
                canonicalizedHeaders = String.Format("x-ms-content-length:1024\nx-ms-date:{0}\nx-ms-type:file\nx-ms-version:{1}", dateInRfc1123Format, msVersion);
            }

            String stringToSign = "";


            //stringToSign = String.Format("{0}\n\n\n\n\n\n\n\n\n\n\n\n{1}\n{2}", requestMethod, canonicalizedHeaders, canonicalizedResource);
            stringToSign = String.Format("{0}\n\n\n\n\n\n\n\n\n\n\n\n{1}{2}", requestMethod, canonicalizedHeaders, canonicalizedResource);

            String authorizationHeader = CreateAuthorizationHeader(stringToSign);

            Uri uri = new Uri(FileEndPoint + urlPath);
            request = (HttpWebRequest)WebRequest.Create(uri);
            if (requestMethod != "Get")
            {
                request.ContentLength = 0;
            }

            request.Method = requestMethod;
            request.Headers.Add("x-ms-date", dateInRfc1123Format);
            request.Headers.Add("x-ms-version", msVersion);
            request.Headers.Add("Authorization", authorizationHeader);
            request.Headers.Add("Accept-Charset", "UTF-8");
            request.Accept = "application/atom+xml,application/xml";
            if (MethodType == "CreateFile")
            {
                request.Headers.Add("x-ms-content-length", "1024");
                request.Headers.Add("x-ms-type", type);

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return request;
    }

我在 CreateAuthorizationHeader 方法中使用 HMAC-SHA256 算法

我的应用程序还具有检索文件共享和目录/文件的逻辑 在存储帐户内并且工作正常。 当我想要创建文件、文件共享或共享快照时,问题就出现了。

任何帮助将不胜感激!!

最佳答案

对于Create Share请求将是:

PUT https://{your-storage-accountname}.file.core.windows.net/{share-name}?restype=share

Request Headers:  
x-ms-version: 2017-04-17 
x-ms-date: <date>  
Authorization: SharedKey {your-storage-accountname}:{shared-key-signature-string}  

规范化的 header 字符串将是:

x-ms-date:Wed, 01 Nov 2017 05:53:50 GMT\nx-ms-version:2017-04-17\n

规范化资源字符串将是:

/{your-account-name}/{your-share-name}\nrestype:share

而对于Create File ,请求将是:

PUT https://{your-storage-accountname}.file.core.windows.net/{share-name}/{file}

Request Headers:  
x-ms-version: 2017-04-17 
x-ms-date: <date>  
x-ms-content-length: 1024 //This header specifies the maximum size for the file, up to 1 TB.
Authorization: SharedKey {your-storage-accountname}:{shared-key-signature-string}  

规范化的 header 字符串将是:

x-ms-content-length:1024\nx-ms-date:Wed, 01 Nov 2017 06:03:57 GMT\nx-ms-type:file\nx-ms-version:2017-04-17\n

规范化资源字符串将是:

/{your-account-name}/{your-share-name}/{your-file}
//e.g. /brucchstorage/myshare1/helloworld.txt

对于您的代码,您需要修改 CreatFileShare 方法下的参数 urlPathcanonicalizedResource 以支持创建文件。此外,您还需要将换行符 (\n) 附加到 GetWebRequest 方法下的 canonicalizedHeaders 参数,以使 MethodType 等于 创建文件

此外,此问题的常见原因是 CanonicalizedResource 和 CanonicalizedHeaders 的生成。我建议您引用Authentication for the Azure Storage Services为了更好地了解它们。此外,您可以利用 fiddler 捕获网络跟踪,并将错误响应中的要签名的字符串与本地值进行比较,以缩小此问题的范围。

正如 Gaurav Mantri 所说,您可以利用适用于 .NET 的 Azure 存储客户端库以简单的方式实现您的目的。更多详情,您可以引用here .

关于c# - HTTP 请求 ' ' 中发现的 MAC 签名与任何计算出的签名不同。服务器使用以下字符串来签名 : 'PUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47025167/

相关文章:

rest - 使用 JWT 对 Symfony 3 API REST 和 Angular webapp 使用 CSRF token 保护有用吗?

java - 如何在 Spring Controller 中自动将 JSON 映射到 Java 类

azure - ASP.Net vnext api 在 localhost 上工作,在 azure 上返回 404

azure - Webjobs 方法索引 - 依赖注入(inject)异常 IConfiguration

c# - 检查两个可枚举之间是否存在共同的单个元素的最快方法

c# - "Access to disposed closure"是什么意思?

c# - 重载的扩展方法在本地有效,但在构建服务器上无效

ruby-on-rails - 如何为状态机或有限自动机实现 RESTful 资源

c# - 获取上传文件的大小 C# MVC

azure - 无需登录即可使用 Azure API(使用角色)