Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get ASP.NET auth cookie for site with CSRF protection enabled using PowerShell
function Get-SessionCookie($url,$username,$password,$cookieName)
{
$loginResponse = Invoke-WebRequest $url -SessionVariable ws -UseBasicParsing
$requestVerificationToken = ($loginResponse.InputFields | Where { $_.name -eq "__RequestVerificationToken" }).value
$body = @{
"__RequestVerificationToken" = $requestVerificationToken;
"UserName" = $username;
"Password" = $password;
}
$loggedInResponse = Invoke-WebRequest $url -Body $body -Method POST -WebSession $ws -UseBasicParsing
return $ws.Cookies.GetCookies($url)[$cookieName].ToString()
}
$cookie = Get-SessionCookie "https://somesite.com/login" "me@me.com" "password" ".ASPXAUTH"
# cookie now contains .ASPXAUTH=xxxxxxx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment