2

Windows Phone アプリを作成していますが、Microsoft.ServiceBus.Messaging.QueueClient クラスを使用できません。

次に、Azure Service Bus REST API を使用して送信しようとしましたが、SAS トークンを作成する必要があります。しかし、SAS トークンを作成するには、Windows.Security.Cryptography.Core.MacAlgorithmNames.HmacSha256 を使用する必要があります。このクラスは先行入力で表示されますが、コンパイル時には存在しません。

Send REST API を使用して Service Bus キューまたはトピックにメッセージを投稿するにはどうすればよいですか?

4

1 に答える 1

3

SAS トークンと HmacSha256 ハッシュの作成を含め、REST API を使用して Service Bus にメッセージを送信する完全なコードを含めます。

固有の Service Bus 名前空間とキーを使用してサンプルを更新する必要があり、おそらくメソッドよりも適切な場所に保存する必要があります...

SAS トークンの作成方法は、http://msdn.microsoft.com/library/azure/dn170477.aspxおよびhttps://code.msdn.microsoft.com/windowsazure/Service-Bus-HTTP-Token-38f2cfc5に記載されています。 .

Windows Phone 8.1の HMACSHA256 から HmacSha256 の実装を取得しましたか?

private static void SendSBMessage(string message)
{
    try
    {
        string baseUri = "https://<your-namespace>.servicebus.windows.net";
        using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
        {
            client.BaseAddress = new Uri(baseUri);
            client.DefaultRequestHeaders.Accept.Clear();

            string token = SASTokenHelper();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("SharedAccessSignature", token);

            string json = JsonConvert.SerializeObject(message);
            HttpContent content = new StringContent(json, Encoding.UTF8);
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            string path = "/<queue-or-topic-name>/messages"; 

            var response = client.PostAsync(path, content).Result;
            if (response.IsSuccessStatusCode)
            {
                // Do something
            }
            else
            {
                // Do something else
            }
        }
    }
    catch(Exception ex)
    {
        // Handle issue
    }
}

private static string SASTokenHelper()
{
    string keyName = "RootManageSharedAccessKey";
    string key = "<your-secret-key>";
    string uri = "<your-namespace>.servicebus.windows.net";

    int expiry = (int)DateTime.UtcNow.AddMinutes(20).Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
    string stringToSign = WebUtility.UrlEncode(uri) + "\n" + expiry.ToString();
    string signature = HmacSha256(key, stringToSign);
    string token = String.Format("sr={0}&sig={1}&se={2}&skn={3}", WebUtility.UrlEncode(uri), WebUtility.UrlEncode(signature), expiry, keyName);

    return token;
}

// Because Windows.Security.Cryptography.Core.MacAlgorithmNames.HmacSha256 doesn't
// exist in WP8.1 context we need to do another implementation
public static string HmacSha256(string key, string value)
{
    var keyStrm = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
    var valueStrm = CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8);

    var objMacProv = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256);
    var hash = objMacProv.CreateHash(keyStrm);
    hash.Append(valueStrm);

    return CryptographicBuffer.EncodeToBase64String(hash.GetValueAndReset());
}
于 2014-12-12T07:49:10.740 に答える