Skip to content

Instantly share code, notes, and snippets.

@jamescrowley
Last active August 29, 2015 13:56
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 jamescrowley/9152282 to your computer and use it in GitHub Desktop.
Save jamescrowley/9152282 to your computer and use it in GitHub Desktop.
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