WebBrowser keep url/uri encoded dont decode - c#

I have a web-browser in a win form application and I am experiencing issues when opening a URL.
The URL I pass in as a new URL instance is encoded with:
/ as %2f , ? as %3f and the
= as %3d
But when I debug my code I can see that the absolute URL or any of the other ones in the webbrowser.url.* is decoded as / , ? and =.
How do I keep the URL encoded? The URL will not work if It is not encoded like that.

I found a solution to my problem, when you have a URL that looks something like this:
domain.com/action/doaction/?identity=12354698789
And you want it encoded like this:
domain.com/logon?returnurl=action%2fdoaction%2f%3fidentity%3d12354698789
That does not work in your web browser. It decodes it to the first url.
I needed the id in the doaction controller so I used this code:
string orgId = ControllerContext.RouteData.Values["id"].ToString();
It returns that url, if unsure, debug and trace through, you will find the right key and value.

Why is it a problem?
If you want the undecoded URL, use the HttpRequest.RawUrl Property. The query string is automatically decoded by default and there is no public parameter that would turn it off.

Related

Why is MVC Controller "return Redirect" decoding my encoded double quotes?

I have a controller that redirects the user. I append a querystring to the URL, but I noticed that the final URL in the browser is decoding my double quotes. So, it shows double quotes in my URL. Is there a way to keep the URL Encoding so it shows %22 instead?
return Redirect(Url.Action("Index", "Search") + "?t=%22test%22");
The url in the browsers shows ?t="test". Anyone know why this is happening? I tried to double encode it, but that created an error message. Thanks!
you can try this
return Redirect(Url.Action("Index", "Search",new {t= "%22test%22" }) );
this will create URL like this %2522test%2522 here %25 means % so in action you can do
string str="%2522test%2522" //you will get this from URL
var value= HttpUtility.UrlDecode(HttpUtility.UrlDecode(str));
i used HttpUtility.UrlDecode two times because first time it will give %22test%22 and second time when decoding %22test%22 you will get "test".

QueryString Out of Encoded URL

I have an encoded URL.
http%3a%2f%myurl.test.me%2fSometjing%2fProduct%2fSearch%3fq=Tomato
I am trying to get query string out of the url which is "Tomato". I am using the following code but it returns null.
var parsedQuery = HttpUtility.ParseQueryString((url));
Console.Write(parsedQuery["q"]); // null
You're missing a few steps. You need to decode the URL, then pull out the query string, and then parse the query string:
string decoded =
HttpUtility.UrlDecode("http%3a%2f%2fmyurl.test.me%2fSometjing%2fProduct%2fSearch%3fq=Tomato");
var uri = new Uri(decoded);
var parsedQuery = HttpUtility.ParseQueryString(uri.Query);
Console.WriteLine (parsedQuery["q"]); // Tomato
Also, your encoded URL is a little malformed. The one in your post decoded looks like this:
http:/%myurl.test.me/Sometjing/Product/Search?q=Tomato
I think you just missed a 2f after the % right before myurl.test:
http%3a%2f%2fmyurl.test.me%2fSometjing%2fProduct%2fSearch%3fq=Tomato
The URL needs to decoded first before you can use the HttpUtility.ParseQueryString().
Fair warning though mentioned directly from MSDN.
The ParseQueryString method uses query strings that might contain user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. MSDN.

Can't get parameter from url via query string

I am implementing google login on my site.
Problem is that when google redirect me back to my site (after confirmation) I can't get access token from query string.
This is URL:
http://localhost/mysite/west/Default.aspx#state=/profile&access_token=ya29.qQDrtcVtgOEbS86Bg10puFG3dksJz74BlrEGulHldlJW2o5qQ6g7ilF17zQsm8iMLG0C82PQyp2Z-g&token_type=Bearer&expires_in=3600
I suspect that this #state=/profile make some issue but can't handle it.
Am I missing something?
If URL is like this , note that there is # after Default.aspx , it is not ?, then there is no direct way to get get querystring ( they are known as URL fragments not querystring), they are meant to be parse at client side and server side don't have access to URL Fragments.
http://localhost/mysite/west/Default.aspx#state=/profile&access_token=ya29.qQDrtcVtgOEbS86Bg10puFG3dksJz74BlrEGulHldlJW2o5qQ6g7ilF17zQsm8iMLG0C82PQyp2Z-g&token_type=Bearer&expires_in=3600
Link contains # ,means an anchor, a position, on a webpage. The browser sends a GET request to the server containing only the address of the entire page, with no anchor, fragment or whatever. When the server returns the page, the browser knows where to position it so the location of the anchor is visible. In clientside or Javascript it is possible as it has access to the anchor.
Read this - How to get Url Hash (#) from server side
You could use
document.URL to get the url.
Then split the url by #state=/profile&
Then the second part of the array split by &.
Then each section split by first =
There may be a more elegant solution but this should work.

C# decodes URL containing %2F on path, is there any way to instruct API to send the URL as it is?

I am having a URL in below format
abcd.com/xyz/pqr%2Fss/abc
I want this to be send to server as it is.
When I build Uri using System.Uri it converts it to abcd.com/xyz/pqr/ss/abc
and it fails as I don't have a URL with the specified path.
When I tried with double encoding
(abcd.com/xyz/pqr%252Fss/abc) it send the Uri as it is but it fails as server side it is converted to (abcd.com/xyz/pqr%2Fss/abc)
If you construct your uri as such:
Uri u = new Uri("http://abcd.com/xyz/pqr%2Fss/abc")
Access the encoded string like this:
u.OriginalString
I had this problem too, but I found the solution: when you use HttpUtility.UrlEncode to be sure that the application will read the url right you have to construct the link this way:
http://www.abcd.com/xyz?val=pqr%2Fss
and not like this
http://www.abcd.com/xyz/pqr%2Fss
where pqr%2Fss is the result of the HttpUtility.UrlEncode("SOME STRING")

Broken encoding after postback

I have a query string with a parameter value that contains the norwegian character å encoded as %e5. The page contains a form with an action attribute which is automatically filled by ASP.Net. When the URL is output into said attribute it is printed with a full two byte encoding: %u00e5.
When posting back this seems to be ok when debugging the code behind. However the page actually does a redirect to itself (for some other reason) and the redirect location header looks like this: Location: /myFolder/MyPage.aspx?Param1=%C3%A5
So the %e5 has been translated to %C3%A5 which breaks the output somehow.
In HTML text the broken characters look like å after having been output via HttpUtility.HtmlEncode.
The entire web application is ISO8859-1 encoded.
PS. When removing the u00 from the output %u00e5 in the action attribute before posting the form, everything is output nicely. But the error seems to be the translation from %e5 to %C3%A5. (And of course the self redirect, but that's another matter.)
Any pointers?
The solution I ended up with was encoding the redirect URL manually.
public void ReloadPage()
{
UrlBuilder url = new UrlBuilder(Context, Request.Path);
foreach (string queryParam in Request.QueryString.AllKeys)
{
string queryParamValue = Request.QueryString[queryParam];
url.AddQueryItem(queryParam, queryParamValue);
}
Response.Redirect( url.ToString(), true);
}
The url.AddQueryItem basically does HttpContext.Server.UrlDecode(queryParamValue) and the url.ToString builds the query string and for each query item does HttpContext.Server.UrlEncode( queryParamValue).
The UrlBuilder is a class already present in our library, so once I found the problem and realized that C#/.Net didn't provide tools for this, coding the fix was quick :)

Categories