Powershell: Webrequest with Basic Authentication

Powershell: Webrequest with Basic Authentication

Theory:

  1. About HTTP Authentication
  2. RFC7617: The 'Basic’ HTTP Authentication Scheme

Practise:

# Credentials
$Username="user"
$Password="pass"

# creating pair string with scheme user:password
$Pair = "$($Username):$($Password)"

# encode pair to ascii bytes octet
$Bytes=[System.Text.Encoding]::ASCII.GetBytes($Pair)

# convert bytes string to Base64
$Base64=[Convert]::ToBase64String($Bytes)

# prepare headet for webrequest
$Header = @{Authorization="Basic $Base64"}

Invoke-WebRequest -Uri $Uri -Method GET -Headers $Header

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany.

*