Skip to content

Instantly share code, notes, and snippets.

@andyhuey
Created April 7, 2023 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyhuey/de85972ec0f6268034e5ce46b0278a07 to your computer and use it in GitHub Desktop.
Save andyhuey/de85972ec0f6268034e5ce46b0278a07 to your computer and use it in GitHub Desktop.
Get the auth hdr and send it to the clipboard.
# get-auth-hdr-1.ps1
# Get the auth hdr and send it to the clipboard.
# ajh 2023-04-06: new.
#Requires -Version 7
#Requires -Modules @{ ModuleName="MSAL.PS"; ModuleVersion="4.0" }
# force TLS 1.2
$TLS12Protocol = [System.Net.SecurityProtocolType] 'Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $TLS12Protocol
echo $null | clip # clear the clipboard.
$secrets = dotnet user-secrets list --json | ConvertFrom-Json
$clientId = $secrets.'AuthConfig:ClientId'
$clientSecret = $secrets.'AuthConfig:ClientSecret'
$secSecret = ConvertTo-SecureString $clientSecret -AsPlainText -Force
$appSettings = Get-Content appsettings.json | ConvertFrom-Json
$scope = $appSettings.AuthConfig.ResourceId
$authority = $appSettings.AuthConfig.Instance -f $appSettings.AuthConfig.TenantId
$msalToken = Get-MsalToken -ClientId $clientId -ClientSecret $secSecret -Scope $scope -Authority $authority
$authHdr = $msalToken.CreateAuthorizationHeader()
$fullAuthHdr = "Authorization: $($authHdr)"
$fullAuthHdr | clip
"auth header has been copied to the clipboard."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment