Search

Showing posts with label Query String. Show all posts
Showing posts with label Query String. Show all posts

May 31, 2008

Duplicate key/value pair in Querystring

I was facing a problem, when the Application_AuthenticateRequest get fails and redirects to login page of the system along with the ReturnUrl which contains the actualy requested url.

Problem was like my ReturnUrl contains the same key twice, which leads to crash in system.

Apart from this, I found in Trace that the ReturnUrl is fine at the requested Page.
like... I made request for http://www.imran/MyProfile.aspx?id=102; it shows 302 as [redirected flag] and the ReturnUrl get the value of http://www.imran/MyProfile.aspx?id=102, but when it goes to Login.aspx it shows 2 query string params.

1. ReturnUrl = http://www.imran/MyProfile.aspx?id=102
2. id=102

Here is the problem, we can see the id is part of ReturnUrl, but in this case it generates following redirection url

Login.aspx?ReturnUrl=http://www.imran/MyProfile.aspx?id=102&id=102

Now when I ask for Request["id"], which gives me value comma saperate like 101,102 and this is what we can say a bug.

After searching long I found the solution, which include the Event that we have to capture in Global.asax, and one property of Response object which sets/gets the Http location Header, here is the code.


protected void Application_EndRequest(object sender, EventArgs e)
{
if (Response.RedirectLocation != null && Response.RedirectLocation.Contains("ReturnUrl"))
{
Response.RedirectLocation = string.Format("{0}ReturnUrl={1}",
Response.RedirectLocation.Remove(Response.RedirectLocation.IndexOf("ReturnUrl")
, (Request.RawUrl.Contains("ReturnUrl") ?
Request.RawUrl.Substring(Request.RawUrl.IndexOf("ReturnUrl") + 10)
: Request.RawUrl
)
));
}
}

This will add the RawUrl into ReturnUrl so there will be no duplication.