【发布时间】:2018-04-12 00:26:00
【问题描述】:
我正在尝试通过 REST API 创建文件共享,但出现此错误
AuthenticationFailed服务器未能验证请求。确保 > > > Authorization 标头的值正确形成,包括签名。 请求编号:aba10b5f-001a-0026-49cb-51d887000000 时间:2017-10-30T22:04:13.9907093ZHTTP请求中找到的MAC>签名>>> 'U8UvWw4KO0fXAk21p/fuXhfqpDdgK7OZn29r5JQ1x4E=' 与任何计算的 > 签名不同。服务器使用以下字符串进行签名:'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 算法
我的应用程序还具有检索文件共享和目录/文件的逻辑 在存储帐户中,它工作正常。 问题出现在我想创建文件、文件共享或共享快照时。
任何帮助将不胜感激!
【问题讨论】:
-
几件事:1)您可以编辑您的问题并包含完整的错误消息吗? 2) 你能包括你正在计算的
stringToSign值的输出吗? 3) 你能包含CreateAuthorizationHeader方法的代码吗?和 4) 你不使用 Azure 存储 SDK 并直接实现 REST API 有什么原因吗? -
@GauravMantri 我查看了 Azure 存储 SDK,现在我可以做我需要的了。谢谢!!
标签: c# rest azure azure-storage-files azure-files