There are a few areas to cover here, so I'll do my best to cover the points. The points are:
- Submitting potentially unsafe text
- Storing unsafe text
- Displaying unsafe text
ASP.NET has a validation mechanism (as pointed out by @Candie) to be a first line defence against an attack. If you have an app that needs to submit HTML, XML, JS etc, you'll have to override the validation to allow it through.
Once the data is through, I would say it is safe to store. The best way to store this data is through the use of Stored Procedures, and not dynamic T-SQL, as it can lead to SQL Injection attacks.
The only problem left now comes from displaying that data verbatim in HTML. If you literally dump content onto the screen, this is where your problems may begin to become more apparent. So this is where your HTMLEncode
comes into play. Characters are converted to HTML equivelant codes, so as not to be a danger. The Literal control offers a .Mode property so the control can handle this for you.
Finally, there is an article on MSDN around How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings, which may also be of use.