我在 ASP NET 4 中遇到以下错误:
A potentially dangerous Request.Path value was detected from the client (<).
我读到我需要在我的 Web.config 中使用 requestValidationMode="2.0":
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime requestValidationMode="2.0" />
</system.web>
所以我的方法是这样的:
[ValidateInput(false)]
public ActionResult Search(string query, string type){
//method body
return result;
}
效果很好。但是,我无法修改我的 Web.config 文件,因为许多其他应用程序依赖于此文件,它可能会导致安全漏洞或其他地方出现错误。
我的应用程序足够安全,可以接受特殊字符,但我不能对正在开发的系统的其余部分说同样的话。 在我的方法中是否可以接受特殊字符作为输入?
例子:
www.mydomain.com/search/query/<myquery<ffoo/type/dept
最佳答案
我在当前项目中遇到了同样的问题。我将发布我必须添加的内容以更正此问题。另外请记住,.NET 4.0 中存在一个错误,该错误用于在 .NET 中工作。 Net 2.0 我的项目是 3.5 但是我认为 IIS AppPool 他们让我们的项目运行 4.0
我在我的网络配置文件中添加了它,它纠正了我遇到的奇怪错误
<httpRuntime requestValidationMode="2.0"
requestPathInvalidCharacters="*,:,&,\"
relaxedUrlToFileSystemMapping="true"
/>
对于 MVC,您可以尝试以下操作
using System.Web.Helpers;
[HttpPost]
[ValidateInput(false)]
public ViewResult Edit(ContentTemplateView contentTemplateView)
{
FormCollection collection = new FormCollection(Request.Unvalidated().Form);
关于c# - asp.net 4 中的潜在危险请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13637035/