Azure Service Bus共享访问签名生成 [英] Azure Service Bus Shared Access Signature Generation

查看:67
本文介绍了Azure Service Bus共享访问签名生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Azure服务总线队列生成共享访问签名.这是我用来生成SAS令牌的代码段.

I am trying to generate Shared Access signature for the Azure service bus queue. Here is the code snippnet through which I am generating SAS token.

using System;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace SASTokenGeneration
{
    class Program
    {
        static void Main(string[] args)
        {
            string resourceUri = "sb://xxxservicequeue.servicebus.windows.net/;SharedAccessKeyName=xxxservicequeue;SharedAccessKey=xxxxxx";
            string key = "token";
            string keyName = "xxxservicequeue";
            try
            {
                TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
                var week = 60 * 60 * 24 * 7;
                var expiry = Convert.ToString((int)sinceEpoch.TotalSeconds + week);
                string stringToSign = HttpUtility.UrlEncode(resourceUri) + "\n" + expiry;
                HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
                var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
                var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
            HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName);

            }
            catch (Exception ex)
            {

            }
        }
    }
}

当我在邮递员中使用此令牌时,有时它会给出

When I use this token in postman then sometime it gives

MalformedToken:无法解析简单的Web令牌.天蓝色服务巴士

MalformedToken: Failed to parse simple web token. azure sevice Bus

现在我得到

401 40103:无效的授权令牌签名

401 40103: Invalid authorization token signature

从错误中我知道连接字符串做错了什么,但无法弄清楚是什么.你能指出我正确的方向吗?谢谢

From error, I know that I am doing something wrong with connection string but cannot figure out what. Can you please point me in right direction. Thanks

推荐答案

问题可能位于

string resourceUri = "sb://xxxservicequeue.servicebus.windows.net/;SharedAccessKeyName=xxxservicequeue;SharedAccessKey=xxxxxx";
string key = "token";
string keyName = "xxxservicequeue";

resourceUri是要求访问的服务总线资源的完整URI,格式为 sb://xxxservicequeue.servicebus.windows.net/或具有您需要的特定实体像 sb://xxxservicequeue.servicebus.windows.net/queueName 一样操作.

The resourceUri is the full URI of the Service Bus resource to which access is claimed, in the format of sb://xxxservicequeue.servicebus.windows.net/ or with specific entity you need to operate like sb://xxxservicequeue.servicebus.windows.net/queueName.

密钥应为 SharedAccessKeyKey 的值,并且keyName是SAS策略名称,如默认的 RootManageSharedAccessKey .

The key should be the value of SharedAccessKeyKey and keyName is SAS policy name like the default RootManageSharedAccessKey.

看看文档以获取更多详细信息.

Have a look at doc for more details.

这篇关于Azure Service Bus共享访问签名生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆