Lâu lâu, trong lúc lập trình ASP.Net xử lý các dữ liệu có yếu tố HTML trên form thì bạn hay gặp lỗi kiểu vầy
Server Error in ‘/’ Application.
A potentially dangerous Request.Form value was detected from the client (ctl00$Content$txtURL=”<iframe width=’750′ …”).
Description: ASP.NET has detected data in the request that is potentially dangerous because it might include HTML markup or script. The data might represent an attempt to compromise the security of your application, such as a cross-site scripting attack. If this type of input is appropriate in your application, you can include code in a web page to explicitly allow it. For more information, see http://go.microsoft.com/fwlink/?LinkID=212874.
Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (ctl00$Content$txtURL=”<iframe width=’750′ …”).
Source Error:
[No relevant source lines]
Source File: c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\vs\cf53654e\69d1dc63\App_Web_w5rduyyu.4.cs Line: 0
Và cách xử lý đơn giản nhất là chèn thêm đoạn ValidateRequest=”false” ở đầu trang aspx là xong. Tuy nhiên, đến phiên bản .NET 4.0 thì có một cải tiến là không chỉ có mỗi trang aspx và code behind của nó mới có thể request validation khi trang aspx đó thực thi mà bây giờ request validation sẽ chạy trước giai đoạn BeginRequest của HTTP request, do đó nó sẽ áp dụng cho tất cả toàn bộ tài nguyên ASP.NET luôn: web service, generic handle, aspx, code behind.
Cũng vì vậy mà bây giờ muốn bỏ qua việc validate request thì ngoài công tác khai báo ở đầu trang aspx còn phải bổ sung thêm đoạn <httpRuntime requestValidationMode=”2.0″ /> vào trong Web.config.
Làm như vậy là ổn, tất nhiên là với điều kiện bạn chắc chắn rằng có thể kiểm soát được dữ liệu đầu vào từ người dùng 🙂