看到类似的帖子,但无法修复我的,
'''
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
1条答案
按热度按时间j9per5c41#
我在我的环境中尝试了下面的代码。
我可以从服务总线获取访问令牌:
感谢
@Roman Kiss
的code reference。示例C#代码:-
**输出:**x1c 0d1x