I'm trying to create a File Share via REST API but I'm getting this error
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
This is my code:
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;
}
I'm using the HMAC-SHA256 algorithm within CreateAuthorizationHeader
method
My application also has logic to retrieve the File shares and Directories/Files inside a storage account and it's working ok. The problem comes at the moment I want to create a File, File share or share snapshot.
Any help would be greatly appreciated!!
stringToSign
value that you're computing? 3) Can you include code forCreateAuthorizationHeader
method? and 4) Is there a reason why you're not using Azure Storage SDK and implementing REST API directly? – Gaurav Mantri