Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save haraldfianbakken/27787c3be40b975364f9 to your computer and use it in GitHub Desktop.
Save haraldfianbakken/27787c3be40b975364f9 to your computer and use it in GitHub Desktop.
Sample code on how to post an xml / base64 encoded file as a SAMLResponse to a server (Saml assertion)
Add-Type -AssemblyName System.Web;
$samlFile = "C:\Tmp\Assertion.xml"
$serviceProviderUri = "http://MYAPP/Sso/Acs"
function Get-SamlPayload($file){
$f = Get-ChildItem $file;
# If the file is an xml, content must be encoded.
if($f.Extension -match ".xml"){
$payload = ([xml](Get-Content $file)).InnerXml;
$payload = [System.Convert]::ToBase64String($payload.ToCharArray());
} else{
$payload = Get-Content $file;
}
# Modify the payload, by encoding invalid characters to post.
$payload = $payload -replace "[+]" , "%2B" -replace "=" , "%3D";
return $payload;
}
$data = ("SAMLResponse={0}" -f (Get-SamlPayload -file $samlFile));
$headers = @{
"Content-Type"= "application/x-www-form-urlencoded";
"Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
"Accept-Encoding"="gzip, deflate";
"Accept-Language"="en-US,en;q=0.8,nb;q=0.6"
"Content-Length"=$data.Length;
};
Invoke-WebRequest $serviceProviderUri -method "POST" -Body $data -Headers $headers;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment