473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Is Html.Encode() method needed?

sanjib65
102 100+
Whenever I take user's input through TextBox or anything else, it's good practice to use Html.Encode(TextBox1.Text) for the security purpose.
But is it neccessary now as ASP.NET 2.0 has strengthened the security measure and in 3.5 it is probably in-built.
I've tried to push html code through TextBox(wjthout Html.Encode()) but Framework resisted it!
Dec 2 '09 #1
4 3173
Frinavale
9,735 Expert Mod 8TB
I'm not sure what the problem is?
Are you trying to upload HTML or JavaScript to a server?

ASP.NET has configurable security settings that check for potential cross site scripting attacks. It restricts certain data from being uploaded to the server. If your application is expecting this type of data then you should cautiously look into configuring the security settings to allow this to be entered.

-Frinny
Dec 2 '09 #2
sanjib65
102 100+
Many thanks Frinny.
Probably I' could not clarify my point clearly and messed up things :)
Actually I have no intention to inject any malicious code to anywhere, what I wanted to know whether Html.Encode() at all neccesssary for my site(especially when I take user inputs) or the .NET Framework has its built-in mrthod to keep a check on this?
Dec 2 '09 #3
Frinavale
9,735 Expert Mod 8TB
It is a good idea to use the Html.Encode() method.

The Encode method transforms any special characters (like <> etc) into the ASCII equivalent.

Let's say the user enters:
<script type="text/javascript">...some nasty script...</script>

It is most likely that ASP.NET will detect this as an attempt at cross site scripting and will throw a security error.

Great you're protected in this instance.

Now let's say that somehow the data stored in the database (maybe from another application or say the security settings for the web applications were configured to allow this) was:

<script type="text/javascript">...some nasty script... </script>

If you send this to the browser as is, the browser will interpret the script between the <script></script> tags and run the code. This could be very bad for you and your end user (the script could be doing anything: redirecting requests to another site first...gathering information and sending it off to some where else...or anything really).

If you had used the Encode method the <> would have been transformed into their ASCII equivalent and the browser would just display the content as text instead of interpreting the text as code and executing it.

Data should never be trusted.
You should always take care to protect yourselves and your client.
The Encode method adds that extra bit of security to ensure that bad things don't happen.

-Frinny
Dec 2 '09 #4
sanjib65
102 100+
Many thanks Frinny, you have explained a very important affair in an excellent manner.
In an hurry I had mistakenly written Html.Encode(), actually it should be either httpUtility.HtmlEncode() or Server.HtmlEncode() method.
Both are required for the untrusted data(whic you have mentioned corrrectly, "Data should never be truate"), that is placed in the Text property.
Dec 3 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Terri | last post by:
I want to send emails that would include a link to an asp page. The link would look like http://10.0.0.10/ContactDetails.asp?ID=18484 How can I prevent someone from simply typing in a...
4
by: Newbie | last post by:
How would I modify this form to encode *all* the characters in the 'source' textarea to the '%xx' format & place result code into the 'output' textarea? (cross browser compatable) Any help is...
4
by: Darrel | last post by:
How does HTML.encode work? I'm trying to save text in a hidden form field into a SQL DB. The tedt is HTML (from a WYSIWYG editor...X-standard). One problem I have is that stray apostrophe's in...
5
by: Steven Berkovitz | last post by:
Hi there, I am having a problem where if i add an attribute to a control at runtime the rendered attribute is HTML encoded. For example, on a textbox: textBox.Attributes = "if(x && y)...
10
by: pak.andrei | last post by:
Here is my script: from mechanize import * from BeautifulSoup import * import StringIO b = Browser() f = b.open("http://www.translate.ru/text.asp?lang=ru") b.select_form(nr=0) b = "hello...
7
by: erikcw | last post by:
Hi, I'm trying to build a SQL string sql = """INSERT INTO ag ('cid', 'ag', 'test') VALUES(%i, %s, %d)""", (cid, ag, self.data) It raises this error: AttributeError: 'tuple' object has no...
6
by: clintonG | last post by:
Can anybody make sense of this crazy and inconsistent results? // IE7 Feed Reading View disabled displays this raw XML <?xml version="1.0" encoding="utf-8" ?> <!-- AT&T HTML entities & XML...
1
Logan1337
by: Logan1337 | last post by:
Hello. I need to take a string in UTF-8 with extended characters (e.g trademark, curly quotes, etc) and encode it for html, with either the html named entities or xml numbered (unicode) entities. ...
4
by: J Peyret | last post by:
Well, as usual I am confused by unicode encoding errors. I have a string with problematic characters in it which I'd like to put into a postgresql table. That results in a postgresql error so I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.