Retrieve BASE64 encryption from AJAX call - c#

I am working on a scraper and I got stuck trying to figure out a base64 encrypted data being passed through AJAX that contains a products Size and Color information.
Sample product URL:
http://merchant.com/MARC-by-Marc-Jacobs-Orion-Metallic-Taffeta-1950s-Dress-Black/prod174690614/p.prod?eVar4=You%20May%20Also%20Like&RST=CategorySiloedViewCP
Using Fiddler, I can see that it calls an AJAX service to retrieve the sizes and colors for this product: http://merchant.com/productserviceAJAX
It passes the following string:
data=$b64$eyJQcm9kdWN0U2l6ZUFuZENvbG9yIjp7InByb2R1Y3RJZHMiOiJwcm9kMTc0NjkwNjE0In19&timestamp=1424678606863
I need to be able to determine how to recreate the above string to be able to pass it to the service and I can get the returned JSON string from the AJAX call.

That's not an encryption, that's an encoding. Decode the base64 string, e.g. using an online tool and look at the result:
{"ProductSizeAndColor":{"productIds":"prod174690614"}}
This is a JSON expression, the product ID appears in your Sample product URL, so you should be able to construct a Base64 string from a given URL using C# Base64 methods.

Related

Decoding querystring values on the server side

I'm running a web service on my server using WCF and .Net 4. The contract type is WebGet. Here is my problem, at one point in time, someone was sending data through the querystring that was URL encoded. So I added HttpUtility.UrlDecode to decode the parameters. I think that fixed my issue at the time. Now, I've sent a URL encoded string to it and I see that the string is being URL decoded coming into the method (before even getting to the HttpUtility.UrlDecode).
So now I'm confused, if the .Net code is decoding it before it gets to my method, why would I need to call on decode explicitly? But for a time it wasn't, so is this a recent change to the underlying .Net framework?
My problem now is that my users are sending data (unencoded), where the data looks like this: "abc%1234" and I'm getting "abc34", the decoding is eating 3 characters. However, if I urlencode the % sign to be "abc%251234", the value coming into the method is "abc%1234" (what I expected) and then the call to HttpUtility.UrlDecode is changing it to "abc34" (which is not what I expected).
I'm not sure how to proceed here. Do I rip out the explicit call to URLDecode until it starts coming across encoded again or is there a better way to handle this?
It's a subtle thing in documentation, easily missed:
HttpRequest.QueryString Property
Property Value
NameValueCollection
The query string variables sent by the client. Keys and values are
URL-decoded.
So if you access the query string via HttpRequest.QueryString (or Params) collection they are already decoded.
You can get to the raw string in RawUrl, QueryString.ToString() (manually that is - re: manipulation, split, etc.).
End of day, %:
Because the percent ("%") character serves as the indicator for
percent-encoded octets, it must be percent-encoded as "%25" for that
octet to be used as data within a URI.
REF: RFC3986
Hth

decode cookie data as json on controller side

I'm receiving on mvc4 razor view cookie. looking on firebug I can see cookie raw data and json formatted data.
I've found that on razor side this should work
#Html.Raw(Json.Encode(object))
How can I on controller side decode raw cookie data to json to examine further.
Is there something like
Json.Encode(Request.Cookies["MyCookie"].Value); // this doesnt work
I recommend using Newtonsoft.Json to convert JSON back and forth; it works great.
You can decode a cookie using below pattern:
JsonConvert.DeserializeObject(Request.Cookies["MyCookie"].Value);
Note:
If you encoded a value and stored it in a cookie, you must decode it before deserilizing. HTTPUtility.UrlDecode() could be used to decode encoded values.
Hope this help.

Accessing Key/Value pairs using HttpListener

EDIT: Please note that this is a WinForms application, not a web app.
I am using the WebClient.UploadValues(Uri, "POST", NameValueCollection) to send values to an instance of HttpListener. On the listener side, when the HttpListener.GetContext() method returns, I can access the sent data as a byte [].
I can convert this data to text using EncodingXXX.GetString(buffer) which returns the following:
Key1=Value1
Key2=Value2
...
Each item in the string is delimited by the ampersand sign &. Both the key and value are encoded using HttpUtility.HtmlEncode/HttpUtility.HtmlDecode so I can split the data based on ampersands fine. The equal = sign, however does not get encoded if the key or value contains it.
The equal sign in the data is to be expected and since HtmlEncode does not take care of it, are there other standard utility classes that can help out? I'd like to avoid manual string replacement if possible since it is error-prone.
It turns out HttpUtility.UrlEncode / HttpUtility.UrlDecode are better suited to this kind of data.

Pass string with Quote in Json result

I wanna send JSON response to browser that requested based on REST service. I use something like this that includes some quotes in Controller Method:
return Json("blah\"blah", JsonRequestBehavior.AllowGet);
And I expect the result would be blah"blah But is blah\"blah and includes back slash too! I wanna have blah"blah in response without any conversion in client side. I know that need to perform this via C# codes but how to do that?
C# and JSON encode characters similarly. The string blah"blah is encoded in both C# and JSON as "blah\"blah". It's perfectly expected, then, that your raw JSON includes the backslash.
When you decode that string with a proper JSON library, it again becomes the string blah"blah.
I found the answer in two above threads:
ASP.Net MVC: how to create a JsonResult based on raw Json Data
How to output Json string as JsonResult in MVC4?
So that I need to use something like this:
return Content(jsonStringFromSomewhere, "application/json");
With this in mind considering that JSONP is used in case of ajax requesting to external service or URL. But I wanna build specific string due to parse with a special parser and I used JSON rather than JSONP and result was great.

ASP.NET MVC Override POST parse of fields

I wrote an MVC action to receive a post from a service. My problem is that the service is posting multipart data with wrong encoding.
Let me give an example:
The service will post the "á" for the form field "text".
I see (using Wireshark), that the byte written on the packet is 225, which is the right byte for "á" in ISO-8859-1 .
I do Request.Form["text"] and actually get a strange (different) char.
I believe this is cause by .NET attempting to convert the value 225 to a unicode char, when converting to string using the utf-8 encoding, but couldn't, as 225 isn't valid for utf-8.
So my question is: Is there a way to override the parsing of those bytes to string?
You could try to add a HttpModule and try to overwrite the ContentEncoding property of the Request object. Though I'm not sure this will work.
It's possible to set the default encoding in Web.Config's GlobalizationSection. The setting is called RequestEncoding and is taken to effect only if the HTTP request of your service does not contain a ContentType header. See http://msdn.microsoft.com/en-us/library/system.web.configuration.globalizationsection.requestencoding.aspx
You can further use inside Web.config to set the above setting only to a specific directory / MVC controller.

Categories

Resources