3

こんにちはすべて私は残りのAPIまたはOdataを使用してWindowsPhoneからAzureテーブルエンティティにアクセスしたいと思います。私はコードを書きましたが、これは私にNULL応答を与えます。テーブルエンティティにアクセスするたびに、GetEntity関数を呼び出します。以下は私が使用しているコードです。誰かがこのコードの何が悪いのか知っているか、助けがあればできるだけ早く返信してください。

////////////GetEntity関数.//////////

     private void GetEntity(String tableName, String partitionKey, String rowKey)
    {
        String requestMethod = "GET";

        String urlPath = String.Format("{0}(PartitionKey='{1}',RowKey='{2}')", tableName,   partitionKey, rowKey);


        String dateInRfc1123Format = DateTime.Now.ToString("R", System.Globalization.CultureInfo.InvariantCulture);

        String canonicalizedResource = String.Format("/{0}/{1}", AzureStorageConstants.Account, urlPath);
        String stringToSign = String.Format(
              "{0}\n\n\n{1}\n{2}",
              requestMethod,
              dateInRfc1123Format,
              canonicalizedResource);
        String authorizationHeader = CreateAuthorizationHeader(stringToSign);
        HttpWebResponse response;
        Uri uri = new Uri(AzureStorageConstants.TableEndPoint + urlPath);


        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
        request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = requestMethod;
        request.Headers[HttpRequestHeader.ProxyAuthorization] = null;
        request.Headers["Address"] = uri.ToString();
        request.Headers["Method"] = requestMethod;
        request.Headers["x-ms-date"]= DateTime.Now.ToString("R", System.Globalization.CultureInfo.InvariantCulture);
        request.Headers["x-ms-version"]= "2011-08-18";            
        request.Headers["Authorization"] = authorizationHeader;
        request.Headers["Accept-Charset"] = "UTF-8";
        request.Headers["ContentType"] = "application/atom+xml,application/xml";
        request.ContentType = "application/atom+xml,application/xml";
        request.Headers["DataServiceVersion"] = "1.0;NetFx";
        request.Headers["MaxDataServiceVersion"] = "1.0;NetFx";

       using (response = GetResponse(request))
        {
            Stream dataStream = response.GetResponseStream();
            using (StreamReader reader = new StreamReader(dataStream))
            {
                String responseFromServer = reader.ReadToEnd();
            }
        }
    }

//GetResponse関数。

       public HttpWebResponse GetResponse(HttpWebRequest request)
       {
        var dataReady = new AutoResetEvent(false);
        HttpWebResponse response = null;
        var callback = new AsyncCallback(delegate(IAsyncResult asynchronousResult)
        {
            response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            dataReady.Set();
        });

        request.BeginGetResponse(callback, request);
            return response;
    }

//////CreateAuthorization関数.///

      private String CreateAuthorizationHeader(String canonicalizedString)
    {
   String signature = string.Empty;
 using (HMACSHA256 hmacSha256 = new HMACSHA256(Convert.FromBase64String(AzureStorageConstants.Key))) 

 {
     Byte[] dataToHmac = System.Text.Encoding.UTF8.GetBytes(canonicalizedString);
     signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
 }

String authorizationHeader = String.Format(
       CultureInfo.InvariantCulture,
       "{0} {1}:{2}",
       AzureStorageConstants.SharedKeyAuthorizationScheme,
       AzureStorageConstants.Account,
       signature);

return authorizationHeader;

}

////////AzureStorageConstants。

      public static class AzureStorageConstants
    {


     private static String TableAccount = "datablobs";
     private static String cloudEndPointFormat = "http://" + TableAccount + ".table.core.windows.net/";


    private static String cloudKey = "Primary Access Key";// Here actual key is written.
    private static string AzureStorage_SharedKeyAuthorizationScheme = "SharedKey";


        public static String Account
        {
            get { return TableAccount; }
        }

        public static string SharedKeyAuthorizationScheme
        {
            get { return AzureStorage_SharedKeyAuthorizationScheme; }
        }

        public static string Key
        {
            get { return  cloudKey; }
        }

        public static String TableEndPoint
        {
            get { return cloudEndPointFormat; }
        }
    }
4

2 に答える 2

2

ソリューションについては、以下のリンクを参照して ください。http://social.msdn.microsoft.com/Forums/en-US/windowsazureconnectivity/thread/84415c36-9475-4af0-9f52-c534f5681432

于 2012-08-03T19:43:39.993 に答える
1

コードを確認したところ、GetEntity()関数には、Windows Azureテーブルストレージにアクセスするための署名の作成に関して問題があることがわかりました。そのため、機能する次のコードを一緒にハックします。GetEntity()を置き換えて、以下のコードに含まれている他の2つの関数を追加するだけで、署名プロセスを適切に機能させることができます。

private string GetEntity(String tableName, String partitionKey, String rowKey)
{
    string result = "";
    String requestMethod = "GET";
    String urlPath = String.Format("{0}(PartitionKey='{1}',RowKey='{2}')",tableName, partitionKey, rowKey);
    DateTime now = DateTime.UtcNow;
    HttpWebResponse response;
    string uri = AzureStorageConstants.TableEndPoint + urlPath;
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
    request.Method = requestMethod;
    request.ContentLength = 0;
    request.Headers.Add("x-ms-date", now.ToString("R", System.Globalization.CultureInfo.InvariantCulture));
    request.Headers.Add("x-ms-version", "2009-09-19");
    request.ContentType = "application/atom+xml";
    request.Headers.Add("DataServiceVersion", "1.0;NetFx");
    request.Headers.Add("MaxDataServiceVersion", "1.0;NetFx");
    request.Headers.Add("If-Match", "*");
    request.Headers.Add("Accept-Charset", "UTF-8");
    request.Headers.Add("Authorization", AuthorizationHeader(requestMethod, now, request));
    request.Accept = "application/atom+xml";
    using (response = request.GetResponse() as HttpWebResponse)
    {
        Stream dataStream = response.GetResponseStream();
        using (StreamReader reader = new StreamReader(dataStream))
        {
            result = reader.ReadToEnd();
        }
    }
    return result;
}

public string AuthorizationHeader(string method, DateTime now, HttpWebRequest request)
{
    string MessageSignature;
    MessageSignature = String.Format("{0}\n\n{1}\n{2}\n{3}",
        method,
        "application/atom+xml",
        now.ToString("R", System.Globalization.CultureInfo.InvariantCulture),
        GetCanonicalizedResource(request.RequestUri, AzureStorageConstants.Account)
        );
    byte[] SignatureBytes = System.Text.Encoding.UTF8.GetBytes(MessageSignature);
    System.Security.Cryptography.HMACSHA256 SHA256 = new System.Security.Cryptography.HMACSHA256(Convert.FromBase64String(AzureStorageConstants.Key));
    String AuthorizationHeader = "SharedKey " + AzureStorageConstants.Account + ":" + Convert.ToBase64String(SHA256.ComputeHash(SignatureBytes));
    return AuthorizationHeader;
}

public string GetCanonicalizedResource(Uri address, string accountName)
{
    StringBuilder str = new StringBuilder();
    StringBuilder builder = new StringBuilder("/");
    builder.Append(accountName);
    builder.Append(address.AbsolutePath);
    str.Append(builder.ToString());
    NameValueCollection values2 = new NameValueCollection();
    NameValueCollection values = HttpUtility.ParseQueryString(address.Query);
    foreach (string str2 in values.Keys)
    {
        ArrayList list = new ArrayList(values.GetValues(str2));
        list.Sort();
        StringBuilder builder2 = new StringBuilder();
        foreach (object obj2 in list)
        {
            if (builder2.Length > 0)
            {
                builder2.Append(",");
            }
            builder2.Append(obj2.ToString());
        }
        values2.Add((str2 == null) ? str2 : str2.ToLowerInvariant(), builder2.ToString());
    }
    ArrayList list2 = new ArrayList(values2.AllKeys);
    list2.Sort();
    foreach (string str3 in list2)
    {
        StringBuilder builder3 = new StringBuilder(string.Empty);
        builder3.Append(str3);
        builder3.Append(":");
        builder3.Append(values2[str3]);
        str.Append("\n");
        str.Append(builder3.ToString());
    }
    return str.ToString();
}

署名関連の問題を修正するために、RESTインターフェイスを介してWindows Azure(Blob、Table、Queue)ストレージにアクセスする優れた実装を持つ Storage_REST_CSサンプルからコードを取得しました。

于 2012-07-01T01:58:51.303 に答える