【发布时间】:2013-09-04 20:19:54
【问题描述】:
我要做的是连接到 Azure Storage Rest API List Blob。参考:http://msdn.microsoft.com/en-us/library/windowsazure/dd135734.aspx
我曾尝试关注http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx 以指定授权标头,但我收到 403 错误 - 禁止。
代码:
Uri address = new Uri("https://account.blob.core.windows.net/$logs?restype=container&comp=list");
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(address);
req.Headers["x-ms-date"] = "2013-09-04";
req.Headers["x-ms-version"] = "2012-02-12";
req.Method = "GET";
string StringToSign = "GET\n"
+ "\n" // content encoding
+ "\n" // content language
+ "\n" // 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
+ "x-ms-date: 2013-09-04\nx-ms-version:2012-02-12\n" // headers
+ "/account/blob\ncomp:list\nrestype:container"; // resources
string accountName = "account";
string key = Convert.ToBase64String(Encoding.Default.GetBytes(StringToSign));
req.Headers["Authorization"] = string.Format("SharedKey {0}:{1}", accountName, key);
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
任何人都可以看到任何错误吗?有没有可以生成密钥的工具?我不确定的一件事是我正在正确编码/散列字符串。
谢谢, 安德鲁
使用最新代码更新。这段代码给了我一个禁止错误。
DateTime dt = DateTime.UtcNow;
string StringToSign = "GET\n"
+ "\n" // content encoding
+ "\n" // content language
+ "\n" // 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
+ "x-ms-date: " + dt.ToString("R") + "\nx-ms-version:2012-02-12\n" // headers
+ "/account/$logs\ncomp:list\nrestype:container";
string auth = SignThis(StringToSign, "accountkey", "account");
string method = "GET";
string urlPath = "https://account.blob.core.windows.net/$logs?restype=container&comp=list";
Uri uri = new Uri(urlPath);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = method;
request.Headers.Add("x-ms-date", dt.ToString("R"));
request.Headers.Add("x-ms-version", "2012-02-12");
request.Headers.Add("Authorization", auth);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
}
【问题讨论】:
-
当你说“有没有可以生成密钥的工具”时,我很怀疑。您是从 azure 管理门户中的 azure 存储页面获取密钥吗?
-
是的。我是 Azure 新手 - 我可以访问门户,我是否打算使用门户中列出的密钥?还是通过上面的代码构造密钥?
-
我现在明白你在用“StringToSign”做什么了。这不是这个例子想要告诉你的。您给出的是结果,而不是要发送的值。让我试着做点什么。拖累的是我有所有这些代码在工作。
-
您应该使用门户中的密钥。与您的帐户名一起的密钥是您的身份验证。这就是为什么您不能将这些密钥分享给所有人的原因。
-
哪个键?订阅号?如果是这样,为什么 MSDN 上面的页面告诉你要完成所有这些步骤?不是拖钓,只是好奇。谢谢