views:

357

answers:

1

I need to access additional querystring parameters from the ReturnUrl querystring.

The entire site requires authentication so all pages will redirect to the login page if the user is not logged in or has timed out. So www.mysite.com/Default.aspx?id=H1234 will redirct to www.mysite.com/login.aspx?ReturnUrl=%2fDefault.aspx%3fid%3dH1234 and if the user then enters their credentials they will be returned to Default.aspx passing the id, but I need to check the id on the login page as well.

On the login page I can Server.UrlDecode(Request.RawUrl) to: /login.aspx?ReturnUrl=/Default.aspx?id=H1234 but as this has 2 question marks I now cannot access Request.QueryString("id")

I could use substring methods to extract the value but that worries me as the id is not a fixed length and it seems insecure.

There doesn't seem to be much on Google (or at least I'm not asking the right question)

A: 

No answers?

This is what I've managed to put together:

Private Function GetParam(ByVal paramName As String, ByVal paramMaxLength As Integer) As String 
Dim paramValue As String = String.Empty Dim url As String = Server.UrlDecode(Request.RawUrl()) 
If Not url.Contains(paramName) Then 
    Return paramValue 
End If 

url = url.Substring(url.LastIndexOf("?") + 1) 
ExtractParamValue(paramName, paramValue, url) 

If paramValue.Length > paramMaxLength Then 
    paramValue = paramValue.Remove(paramMaxLength) 
End If 

Return paramValue

End Function

and

Private Shared Sub ExtractParamValue(ByVal paramName As String, ByRef paramValue As String, ByVal url As String)
For Each valuePair As String In url.Split("&")
    If valuePair.StartsWith(paramName) Then
        paramValue = valuePair.Substring(paramName.Length + 1)
    End If
Next

End Sub

Simon Martin

related questions