3

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();
Andi
  • 41
  • 7
  • Which line of code is giving you this error? Also, can you edit your question and include the complete error message? – Gaurav Mantri Sep 26 '22 at 06:50
  • @GauravMantri, I have posted the error in italics, The code is not failing at one line, some times it is failing at accessing the share/ Directory / sub directory / file. it is working if I do test after 30 minutes or if I change the access key in the connection string – Andi Sep 26 '22 at 07:00

1 Answers1

1

if (!share.Exists()) is not working and we need to write as share.CreateIfNotExists(). It solved the Authentication error.

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.

Andi
  • 41
  • 7
Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • Thank you @Gaurav Mantri, for finding the error, my machine date time is 9/26/2022. Is there any way to force the request date – Andi Sep 26 '22 at 07:52
  • it is working once in 5 to 6 times. is there any ShareClient session to close – Andi Sep 26 '22 at 08:01
  • `is there any ShareClient session to close` - no. Is there something else that's running on your machine that might change the date/time? – Gaurav Mantri Sep 26 '22 at 09:51
  • Finally I found the Answer for this error, if (!share.Exists()) is wrong and we need to write as share.CreateIfNotExists(). It solved the Authentication error. Thank you @Gaurav. – Andi Sep 27 '22 at 04:03