使用 ASP.NET 进行 HTML 编码
我目前在插入/更新数据库表记录之前对所有用户输入的文本进行 html 编码。问题在于,在任何后续更新中,先前编码的字符串都会被重新编码。这个无限循环开始占用我表中的大量列空间。我正在对所有 sql 语句使用参数化查询,但我想知道让 .NET Framework 处理这部分而不使用 HTML 编码是否安全?
I am currently html encoding all user entered text before inserting/updating a db table record. The problem is that on any subsequent updates, the previously encoded string is reencoded. This endless loop is starting to eat up alot of column space in my tables. I am using parameterized queries for all sql statements but am wondering would it be safe to just let the .NET Framework handle this part without the HTML Encoding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该始终在显示时对用户数据进行 HTML 编码,而不是在存储时进行编码。将用户输入保存在数据库中(使用参数化查询或其他方式来防止 SQL 注入),然后在输出数据时进行 HTML 编码。这样你就永远不会遇到这个问题。
HTML 编码非常简单地内置于 ASP.NET 框架中。您可以这样做:
You should always HTML encode user data upon displaying, never upon storing. Save the user input in DB (using parametrized queries or whatnot to prevent SQL injection) and then HTML encode when outputting the data. That way you'll never have this problem.
HTML encoding is built into the ASP.NET framework real simply. This is how you do it:
我不建议对数据库中的数据进行编码。
编码与数据无关,但它专门针对您显示数据的方式。如果您希望客户端应用程序将来使用此数据或其他非 HTML 显示,该怎么办?
您应该将数据作为原始数据存储在表和应用程序中,或者服务应用程序应将编码处理为所需的任何格式的层。
.NET 框架可以轻松地为您做到这一点。只需记住使用
HtmlEncode
或在 ASP.NET 4<%:
中即可。您应该对需要呈现的任何动态数据执行此操作。将其编码存储在数据库中不仅会在今天给您带来问题,而且在将来也会给您带来问题。
I wouldn't recommend encoding the data in the database.
The encoding has nothing to do with the data but it specifically targetted at how you are displaying the data. What if you want a client app to use this data in the future or some other non-HTML display?
You should be storing the data as the raw data in your tables and the applications, or the layer that services applications should handle the encoding to whatever formats are required.
The .NET framework can easily do it for you. Just remember to use
HtmlEncode
or in ASP.NET 4<%:
. You should be doing this for ANY data that you need to present that is dynamic.Storing it in the database encoded will not only cause you problems today but on going in the future.
您可以使用 encode 保存输入,并在更新时对其进行解码,然后更新它并再次使用 encode 保存,并且在显示时不需要执行任何操作。 ..
这将带来一个好处..不需要在演出时一次又一次地编码...
但问题可能是您想在 rowdatabound 进行更改,然后您必须解码然后更改并再次编码:) :) 快乐编码
you can save input with encode , and at the time of update decode it then update it and again save using encode and at the time of show do not need to do anything...
this will give one benefit .. do not need to encode again and again at show time...
but a problem may be you want to change at rowdatabound then u would have to decode then change and encode again :) :) happy coding