Quick PowerShell cmdlet to get query parameters sent to localhost

When testing Azure AD applications or showing of things such as implicit flow, authorization code flow etc., it can be very useful to have a listener going on localhost in order to provide the browser a valid redirect url. The following cmdlet can be used to do this, without the need to install anything.

function Get-HttpQueryParametersSentToLocalhost
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [int] $Port = 8080,

        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=1)]
        [string] $Response = "Done"
    )

    $listener = New-Object System.Net.HttpListener
    $listener.Prefixes.Add("http://localhost:$Port/")
    Write-verbose "Waiting for request at http://localhost:$Port/"
    $listener.Start()
    $context = $listener.GetContext()
    $Content = [System.Text.Encoding]::UTF8.GetBytes($Response)
    $Context.Response.OutputStream.Write($Content, 0, $Content.Length)
    $Context.Response.Close()
    $listener.Dispose()
    $Context.Request.RawUrl -split "[?&]" -like "*=*" | foreach -Begin {$h = @{}} -Process {$h[($_ -split "=",2 | select -index 0)] = ($_ -split "=",2 | select -index 1)} -End {$h}
    
}

$parameters = Get-HttpQueryParametersSentToLocalhost -Verbose -Port 8080
$parameters | Out-GridView

How, try to do to http://localhost:8080/asd/?test=a&idtoken=asdd&code=123 and check out what happens in PowerShell. 🙂

Ill create a new blog post that uses this to demonstrate different OpenID Connect flows later.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s