Skip to content

Instantly share code, notes, and snippets.

@fabiocannas
Created August 7, 2024 13:10
Show Gist options
  • Select an option

  • Save fabiocannas/dc2ee8c6fd54ee94f29034d7b40a905d to your computer and use it in GitHub Desktop.

Select an option

Save fabiocannas/dc2ee8c6fd54ee94f29034d7b40a905d to your computer and use it in GitHub Desktop.
Creation of a Service Bus Queue SAS Token
[Reflection.Assembly]::LoadWithPartialName("System.Web")| out-null
$URI="<sb_namespace_uri>/<queue_name>"
$Access_Policy_Name="<access_policy_name>"
$Access_Policy_Key="<access_policy_key>"
$targetDate = [DateTime]::ParseExact("<date_dd/MM/yyyy>", "dd/MM/yyyy", $null).Date.AddHours(23).AddMinutes(59).AddSeconds(59).AddMilliseconds(999)
# Convert the target date to Unix timestamp
$Expires= [DateTimeOffset]::new($targetDate).ToUnixTimeSeconds()
$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
$SASToken
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment