Skip to content

Instantly share code, notes, and snippets.

@jamescrowley jamescrowley/GetCookie.ps1
Last active Aug 29, 2015

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 { $ -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 "" "" "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
You can’t perform that action at this time.