When adding user input to a web page, it should (unless it's HTML of course :) be encoded to help prevent XSS attacks etc.. like this:
litForename.Text = HttpUtility.HtmlEncode(MyUser.Forename);
I'm putting together a template to generate my business logic layer, and I'm thinking of using it to do all the encoding as soon as the data comes out of the database, before it gets to the UI code. This will ensure that everything is encoded that should be (I'd obviously exclude the columns that contain Xhtml/Xml strings). An overload on the data access methods will allow retrieval of data with no encoding (so it can be edited):
// Get a 'User' entity with all the string fields HTML encoded
BLL.Users.GetById(int userId)
// Get a 'User' entity with optional HTML encoding
BLL.Users.GetById(int userId, bool useHtmlEncoding)
Is this an approach that anyone else uses, or is it a dumb idea? What are the pros and cons?
Thanks.