Web Services 访问令牌格式不正确

ndasle7k  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(152)

看到类似的帖子,但无法修复我的,
'''

const sharedKeyName = "RootManageSharedAccessKey"
const sharedKey = "xxx"
const uri = "https://d365-service-bus-dev.servicebus.windows.net/XX-XX/subscriptions/XXX/messages/head"

function createSharedAccessToken(uri, saName, saKey) { 
    if (!uri || !saName || !saKey) { 
            throw "Missing required parameter"; 
        } 
    var encoded = encodeURIComponent(uri); 
    var now = new Date(); 
    var week = 60*60*24*7;
    var ttl = Math.round(now.getTime() / 1000) + week;
    var signature = encoded + '\n' + ttl;
    const hash = CryptoJS.HmacSHA256(signature, saKey).toString(CryptoJS.enc.Base64)
    return 'SharedAccessSignature sr=' + encoded + '&sig=' +  
        encodeURIComponent(hash) + '&se=' + ttl + '&skn=' + saName; 
}

// Set broker properties e.g. sessionId
const brokerProperties = {
    'SessionId':'123'
}

// Set broker proerties variable
console.log() 
pm.variables.set("broker_properties", JSON.stringify(brokerProperties));
// Set access token variable
pm.variables.set('access_token', createSharedAccessToken(uri, sharedKeyName, sharedKey));

'''
我试图在C#中这样做,但令牌总是畸形即使我比较 Postman (JS)和我生成的一个,他们看起来像,但我相信编码的东西我失踪,

if (string.IsNullOrEmpty(uri) || string.IsNullOrEmpty(saName) || string.IsNullOrEmpty(saKey))
            {
                throw new ArgumentException("Missing required parameter");
            }
        
            string encoded = Uri.EscapeDataString(uri);
            DateTime now = DateTime.UtcNow;
            int week = 60 * 60 * 24 * 7;
            long ttl = (long)(now - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds + week;
        
            string signature = encoded + "\n" + ttl;
            byte[] keyBytes = Convert.FromBase64String(saKey);
            using (var hmac = new HMACSHA256(keyBytes))
            {
                byte[] signatureBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(signature));
                string hash = Convert.ToBase64String(signatureBytes);
                Token ="SharedAccessSignature sr="+encoded+"&sig="+Uri.EscapeDataString(hash)+"&se="+ttl+"&skn="+saName;
            }
            Console.WriteLine(Token);
    ```

What I'm missing here

Regards 
HAzem
j9per5c4

j9per5c41#

我在我的环境中尝试了下面的代码。
我可以从服务总线获取访问令牌:
感谢@Roman Kisscode reference

示例C#代码:-

TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
var expiry = Convert.ToString((int)sinceEpoch.TotalSeconds + 3600);
string stringToSign = Uri.EscapeDataString(uri) + "\n" + expiry;
HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(saKey));
var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
var sasToken = String.Format("SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", Uri.EscapeDataString(uri), Uri.EscapeDataString(signature), expiry, saName);
return sasToken;
string uri = "https://d365-service-bus-dev.servicebus.windows.net/XX-XX/subscriptions/XXX/messages/head";
string sharedKeyName = "xxxx";
string sharedKey = "xxxx";
string sessionId = "123";
string sasToken = CreateSharedAccessToken(uri, saName, saKey);
var brokerProperties = new
{
    SessionId = sessionId
};
Console.WriteLine();
Console.WriteLine("Setting broker_properties variable...");
Console.WriteLine();
Console.WriteLine(JsonConvert.SerializeObject(brokerProperties));
Console.WriteLine();
Console.WriteLine("Setting access_token variable...");
Console.WriteLine();
Console.WriteLine(sasToken);

**输出:**x1c 0d1x

相关问题