Azure服务总线发送消息-授权令牌签名无效

snvhrwxg  于 2023-10-22  发布在  其他
关注(0)|答案(2)|浏览(110)

我有下面的powershell代码来向Azure服务总线队列发送消息。

param
(
    [Parameter(Mandatory = $true)][string] $ResourceGroupName,
    [Parameter(Mandatory = $true)][string] $NamespaceName,
    [Parameter(Mandatory = $true)][string] $QueueName,
    [Parameter(Mandatory = $false)][string] $PolicyName = 'SASPolicy'
)      
$test_msg = [PSCustomObject] @{ "test" = "This is a test message" } | ConvertTo-Json -Compress
$authorizationId = (Get-AzServiceBusAuthorizationRule -ResourceGroupName $ResourceGroupName -NamespaceName $NamespaceName -QueueName $QueueName).Id
$token = (New-AzServiceBusAuthorizationRuleSASToken -AuthorizationRuleId $authorizationId  -KeyType PrimaryKey -ExpiryTime Get-Date.AddHours(1.0)).SharedAccessSignature 

$auth_headers = @{ "Authorization" = "SharedAccessSignature $token"; "Content-Type" = "application/json" }
$uri = "https://$NamespaceName.servicebus.windows.net/$QueueName/messages"
$http_statuscode = Invoke-WebRequest -Uri $uri -Headers $auth_headers -Method Post -Body $test_msg
Write-Host "Message sent successfully: $http_statuscode.StatusCode"

我在invoke-webrequest上收到以下错误
响应状态代码不表示成功:401(子代码=40103:|授权令牌签名无效)。

ruoxqz4g

ruoxqz4g1#

我在我的环境中尝试了一下,得到了以下结果:

最初,当我尝试使用你的代码时,我得到了同样的错误:

当您在授权标头中传递错误的SAS令牌时,会发生上述错误。
您可以按照此MS-DOCS并使用下面的PowerShell代码将消息发送到Azure服务总线队列。

验证码:

$QueueName='yourqueuename'
$NamespaceName='Yournamespacename'
$Access_Policy_Name="your-access policyname"
$Access_Policy_Key="your-access-policy key"

$test_msg = [PSCustomObject] @{ "test" = "This is a test message" } | ConvertTo-Json -Compress 

$uri = "https://$NamespaceName.servicebus.windows.net/$QueueName"   
$Expires=([DateTimeOffset]::Now.ToUnixTimeSeconds())+300
$SignatureString=[System.Web.HttpUtility]::UrlEncode($URI)+ "`n" + [string]$Expires
$HMAC = New-Object System.Security.Cryptography.HMACSHA256
$HMAC.key = [Text.Encoding]::ASCII.GetBytes($Access_Policy_Key)
$Signature = $HMAC.ComputeHash([Text.Encoding]::ASCII.GetBytes($SignatureString))
$Signature = [Convert]::ToBase64String($Signature)
$SASToken = "SharedAccessSignature sr=" + [System.Web.HttpUtility]::UrlEncode($URI) + "&sig=" + [System.Web.HttpUtility]::UrlEncode($Signature) + "&se=" + $Expires + "&skn=" + $Access_Policy_Name

$auth_headers = @{ "Authorization" = "$SASToken"; "Content-Type" = "application/json ";}
$uri = "https://$NamespaceName.servicebus.windows.net/$QueueName/messages"
$http_statuscode = Invoke-WebRequest -Uri $uri -Headers $auth_headers -Method Post -Body $test_msg
Write-Host "Message sent successfully"

输出:

Message sent successfully

入口:

参考:

使用共享访问签名进行Azure服务总线访问控制- Azure服务总线|微软学习

bwntbbo3

bwntbbo32#

我的名字是错误的,即。PrimaryKey应替换为“SASPolicy”,这是在总线上定义的SAS策略的名称。

$token = (New-AzServiceBusAuthorizationRuleSASToken -AuthorizationRuleId $authorizationId  -KeyType PrimaryKey -ExpiryTime Get-Date.AddHours(1.0)).SharedAccessSignature

$token = (New-AzServiceBusAuthorizationRuleSASToken -AuthorizationRuleId $authorizationId  -KeyType SASPolicy -ExpiryTime (Get-Date).AddHours(1.0)).SharedAccessSignature

相关问题