ASP.NET Web API and UTF16 encoding - c#

I have a ASP.NET web API running and have a client running into following issue:
{"model":["Unable to translate bytes [E9]
at index 300 from specified code page to Unicode."]}
The exception appears to be happening during model binding, which is the default nothing custom on my end.
The client is also running .NET and they say that the content is being sent over as UTF16....but they are not including HTTP header to indicate that. Up until they began having special characters in their content everything worked great.
My question is how to deal with this. Initially I was going to add a DelegateHandler which would detect this client and somehow set content-type/charset to UTF16 but that isn't panning out. I then thought I could transcode the request body to UTF8 from within DelegateHandler but have read that modifying content in handler is not possible or recommended.
Since the issue appears to be during model binding I'm wondering if implementing a custom model binder would be in order?
Thanks for any help!

The default encoding used by the built-in formatters is UTF-8. You can easily change it to UTF 16. You can change this on a per-formatter basis by changing the SupportEncodings property on the MediaTypeFormatter class.
This link can help you.

Related

Microsoft azure service bus api - How to add AutoDeleteOnIdle on subscription description

I am trying to make a small library that uses service bus API client.
I followed this tutorial for achieving this goal and I manage to do almost every thing I needed except one thing, add AutoDeleteOnIdle property for newly created subscriptions.
Thing is, on the tutorial, the author uses xml body to send to the API and it works well, but I want to add inside the xlm SubscriptionDescription the property AutoDeleteOnIdle, but I keep receiving bad request with the response «The specified resource description is invalid».
I attempted to search on the net about this xml structure and how to find it.. But I can't find any doc!! Even if I check the shema specified in the xml, I get redirected to an unexisting page (for http://schemas.microsoft.com/netservices/2010/10/servicebus/connect).
I am using Microsoft.Azure.ServiceBus v.3.4.0 NuGet, visual studio 2017.
This is the subscription description I've been trying to add AutoDeleteOnIdle property.
Encoding.UTF8.GetBytes("<entry xmlns='http://www.w3.org/2005/Atom'><content type='application/xml'>"
+ "<SubscriptionDescription xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.microsoft.com/netservices/2010/10/servicebus/connect\">"
+ "</SubscriptionDescription></content></entry>");
The code is on this page: https://code.msdn.microsoft.com/Service-Bus-HTTP-client-fe7da74a
Thank you for your help,
Phil
I found out what was the problem, I didn't know that I needed to type a special format XSD dates for xml body.
I was adding <AutoDeleteOnIdle>00:10:00</AutoDeleteOnIdle> because the doc says that they need a ISO format, but it was actually:
<AutoDeleteOnIdle>PT10M</AutoDeleteOnIdle> I should have sent.

Handle angular bracket in ASP.Net

Currently when trying to submit transaction that contain dropdown that has text containg angular bracket as e.g. "<abcd>", I'm getting 500 internal server error since ValidateRequest=true by default and throws unhandled exception before it reached to page handler since its Cross site scripting problem.
Is there a way to intercept and modify request object in HttpModule or Glabal.asax since I know Request object is readonly.
I've been breaking head for almost three days but not able to get a concrete solution.
What would be the best solution to handle these kind of scenarios. Also I don't want to encode it in client side.
Encode the value in the listbox :
<asp:ListItem><abcd></asp:ListItem>
[Edit] I realize my solution does not apply. The content is probably sent encoded by the browser, and is render encoded too by the drop down list (if you use standard databinding). I think your only option is to disable the validation of the request.
This implies you have to very careful on user input. To be simple, Encode every user input with HttpUtility.HtmlEncode(txtXX.Text);.
More on this in the Script Exploits Overview page of MSDN.
Use HttpUtility.HtmlEncode and HttpUtility.HtmlDecode to solve this problem..
you can put html encoded text in the control or use these methods as per your requirement( Between Events)
Follow these SO thread...
HttpUtility.HtmlEncode to validate user entries
w3c validation error in asp.net
If you can disable validation then follow these
asp.net: Invalid postback or callback argument
Check MSDN for Script Exploits
I prefer to use Microsoft Anti-Cross Site Scripting Library V4.0 as it provides many helper functions to encode HTML, HtmlAttribute, JavaScript, URL, XML to restrict any cross site attacks.

how to download a partial web page using .net

We are downloading a full web page using System.Net.WebClient class. But we only want less than half of the page. So is there a way to download a portion of the page, say 1/3rd, half etc of a page using .net library so that We can save the network bandwidth and the space? If so, please throw your ideas, thanks.
You need to provide an "Accept-Ranges header" to your GET or POST request. That can be done by using the AddRange method of your HttpWebRequest:
HttpWebRequest myHttpWebRequest =
(HttpWebRequest)WebRequest.Create("http://www.foo.com");
myHttpWebRequest.AddRange(0,100);
That would yield the first 100 bytes. The server, however, needs to support this.
The sort answer is not unless the web app supports some way to tailor it's response to what you want it to return.
This could take the form of a
query string parameter
header field value
The simplest way to add this would be a query string parameter and when detected write out the necessary HTML to the response object. If you are unable to make changes to the web app then you won't be able to control how much of a page is returned to you.
You might want to read up on how HTTP works since the question and it's answer relies upon this. Specifically the Header Definition should be helpful.

How Design A Software to InputData In WebForm Automatically?

I Want Create A Software to Input Data in WebForms Automatically (like Robot) And Accept Input Data.
How I Can Create this Software in C# (Windows Application)?
what Technologies Must Be Used?
What OpenSource Project Exist for use?
Sample Code And etc...
Please Help Me
I hope you're doing something within the acceptable terms of use with the content you automatically post. Ie. you do not ask how to create yet another spam bot...
To grab the HTTP form you can use WebRequest. This returns the content of the page (including the form) as a response stream. You can then parse the response using HtmlAgility pack, for the forms you are interested. Once you know the forms and fields in the page, you can set values for the fields and post a response, again using a WebRequest but changing the method to POST and encoding the reponse fields as application/x-www-form-urlencoded content, see How to: Send Data Using the WebRequest Class.
This method is using almost the most basic building blocks, going lower level than this would mean using sockets and formating the HTTP request yourself. At this low level you'll have a great deal of freedom and flexibility on how to parse the form and send back the request, at the cost of actually having to understand how WebForms and HTTP work.

ASP.NET MVC Alter Markup before Output

Excuse my limited knoweldge here.
In the past I have used Steve Sanderson's method to HTML encode by default at runtime: http://blog.stevensanderson.com/2007/12/19/aspnet-mvc-prevent-xss-with-automatic-html-encoding/
I have a need to alter img src and a href attributes before they are spat out in the user's browser. There is a solution using JavaScript but this is not ideal for several reasons. Intercepting the compiler is not an option because of unnecessarily using Response.Write for trivial HTML.
Is there something I can do with HTTP modules or the view engine?
Any thoughts?
UPDATE: I do not need to HTML encode the attributes but I do have a need to change them.
Cheers.
Use a response filter. Works with any ASP.NET project, including MVC. Should work even if you're using a different view engine, as it intercepts at a lower level.
Here's an actual example that strips whitespace:
https://web.archive.org/web/20211029043851/https://www.4guysfromrolla.com/articles/120308-1.aspx
I've used this before to rewrite links before sending to the client, but I can't find the code at the moment.

Categories