Skip to content

Instantly share code, notes, and snippets.

@anderseide
Last active August 5, 2019 11:08
Show Gist options
  • Save anderseide/4fec73811b2e87d210cfe4faced4dac7 to your computer and use it in GitHub Desktop.
Save anderseide/4fec73811b2e87d210cfe4faced4dac7 to your computer and use it in GitHub Desktop.
Generate Event Hub SAS token
[Reflection.Assembly]::LoadWithPartialName("System.Web")| out-null
$URI="[ADD_YOUR_EVENT_HUB_NAMESPACE].servicebus.windows.net/myeventhub"
$Access_Policy_Name="RootManageSharedAccessKey"
$Access_Policy_Key="[ROOT_MANAGED_SHARED_ACCESS_KEY]"
#Added $UntilTokenExpireInSeconds instead of the 300 seconds example from docs.microsoft.com
$UntilTokenExpireInSeconds = 60*60*24*365
$Expires=([DateTimeOffset]::Now.ToUnixTimeSeconds())+$UntilTokenExpireInSeconds
$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
Write-Output $SASToken
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment