简体   繁体   中英

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

I am using the below code to copy the file to Azure File share. It is throwing error and Some times its working when I do

  1. like reset the key by replacing the new key in connectionstring
  2. by changing the time from cst and est in local machine( not all time working)

I am getting Error as

Azure.RequestFailedException: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:b6a51c3f-b01a-0037-7774-d19906000000 Time:2022-09-26T06:53:52.3042347Z Status: 403 (Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.) ErrorCode: AuthenticationFailed Additional Information: AuthenticationErrorDetail: The MAC signature found in the HTTP request 'Xrp6MB0=' is not the same as any computed signature. Server used following string to sign: 'GET Thu, 22 Sep 2022 10:07:55 GMT "0x8DA9C8251A37348" x-ms-client-request-id:25ff53d8-7a55-414a-954b-ff8f6d05bced x-ms-date:Mon, 26 Sep 2022 06:53:52 GMT x-ms-return-client-request-id:true x-ms-version:2021-08-06

Content: <xml version="1.0" encoding="utf-8"?>AuthenticationFailedServer failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:b6a51c3f- Time:2022-09-26T06:53:52.3042347ZThe MAC signature found in the HTTP request 'Xrp6MB0=' is not the same as any computed signature. Server used following string to sign: 'GET

I am using Access key 1 to access the Azure File share

Could someone let me know what is the error I am doing in the code

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();

I believe the error is coming because of the date/time settings on the computer where your code is running.

If you notice the error message, the date/time returned by Azure Storage Service is 2022-09-26T06:53:52.3042347Z however the date/time value sent in the request is Thu, 22 Sep 2022 10:07:55 GMT .

Because the current date/time on the machine where your code is running is about 4 days before the date/time on Azure Storage Service, the requests to Azure Storage are being rejected.

I would recommend taking a look at the date/time value on the local machine and make sure that it is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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