Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Blob服务Rest API执行附加块操作-403身份验证失败_C#_Azure_Blob - Fatal编程技术网

C# 使用Blob服务Rest API执行附加块操作-403身份验证失败

C# 使用Blob服务Rest API执行附加块操作-403身份验证失败,c#,azure,blob,C#,Azure,Blob,我正在调查一个错误,这个错误只发生在我的应用程序中的Put Append操作中,由于RESTAPI,它与Azure Blob服务通信 文件的创建已成功,但由于它是一个append blob,我必须使用append Block操作添加内容,当我尝试这样做时,我得到一个403 Forbidden WebException: <?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed

我正在调查一个错误,这个错误只发生在我的应用程序中的Put Append操作中,由于RESTAPI,它与Azure Blob服务通信

文件的创建已成功,但由于它是一个append blob,我必须使用append Block操作添加内容,当我尝试这样做时,我得到一个403 Forbidden WebException:

<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:00dc0116-0001-00c2-066b-cbafe8000000
Time:2017-05-12T22:01:16.1689598Z</Message><AuthenticationErrorDetail>The MAC signature found in the HTTP request 'N7UVKFwftf2YnAFdnciRneu7LsAkWHKXUpwhFRxlQqI=' is not the same as any computed signature. Server used following string to sign: 'PUT


800








x-ms-date:Fri, 12 May 2017 22:01:15 GMT
x-ms-version:2016-05-31
/<myaccount>/write/FixedRecord10r.txt
comp:appendblock'.</AuthenticationErrorDetail></Error>
但是,在构造签名的方法中,我显示了stringToSign,它与使用的服务器完全相同

PUT


800








x-ms-date:Fri, 12 May 2017 22:01:15 GMT
x-ms-version:2016-05-31
/<myaccount>/write/FixedRecord10r.txt
comp:appendblock

我现在想知道我是否错过了什么,或者这个特定的操作是否有问题,因为我从来没有遇到过其他操作的问题。

我注意到这里有一个额外的空间(在
\n
之间):


请删除此空间并重试您的请求。

我注意到此处有一个额外的空间:
{path+=“\ncomp:appendblock”;}
(介于
\n
之间)。你能删除它并再次尝试你的请求吗?额外的空间是问题所在。。。非常感谢你!
public void AppendFile(MemoryStream stream)
{
 string dateFormatted = string.Format(CultureInfo.InvariantCulture, "{0:R}", DateTime.UtcNow);
 string signature = GetSignature("PUT", "xxxxxxx", afsAccount, dateFormatted, null, null, stream.Length, null);

 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://" + afsAccount + ".blob.core.windows.net" + path + "?comp=appendblock");
 request.Headers.Add("x-ms-version", "2016-05-31");
 request.Headers.Add("x-ms-date", dateFormatted);
 request.Headers.Add(HttpRequestHeader.Authorization, "SharedKey " + afsAccount + ":" + signature);
 request.Method = "PUT";
 request.ContentLength = stream.Length;

 using (var requestStream = request.GetRequestStream())
 {
      stream.Position = 0;
      stream.CopyTo(requestStream);
 }

 try
 {
  using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()){
...
}


    private String GetSignature(string verb, string azureAccessKey, string afsAccount, string date, string byteRange, string containerName, long contentLength, string blobType)
    {
        if(byteRange != null)
        {
            byteRange = "x-ms-range:" + byteRange + "\n";
        }

        string path = "/" + afsAccount + Dfs.Path;
        if (containerName != null)
        {
            path = "/" + afsAccount + "/" + containerName + "\nrestype:container";
        }else if (contentLength > 0)
        {
            path += " \ncomp:appendblock";
        }

        string length = "\n";
        if (contentLength != 0)
        {
            length = contentLength.ToString() + "\n";
        }

        if (blobType != null)
        {
            blobType = "x-ms-blob-type:" + blobType + "\n";
        }

        // construct input value
        string inputValue = verb + "\n" +
          "\n" + /*Content-Encoding*/
          "\n" + /*Content-Language*/
          length + /*Content-Length*/
          "\n" + /*Content-MD5*/
          "\n" + /*Content-Type*/
          "\n" + /*Date*/
          "\n" + /*If-Modified-Since*/
          "\n" + /*If-Match*/
          "\n" + /*If-None-Match*/
          "\n" + /*If-Unmodified-Since*/
          "\n" + /*Range*/ 
          blobType +
          "x-ms-date:" + date + "\n" +
          byteRange +
          "x-ms-version:2016-05-31\n" +
          path;

        Console.WriteLine(inputValue);

        // create base64 encoded signature
        var hmac = new HMACSHA256();
        hmac.Key = Convert.FromBase64String(azureAccessKey);
        byte[] sigbyte = hmac.ComputeHash(Encoding.UTF8.GetBytes(inputValue));
        var signature = Convert.ToBase64String(sigbyte);

        return signature;
    }
{
    path += " \ncomp:appendblock";
}