C#ASP.NET MVC手动访问Request.Form&潜在危险值 [英] C# ASP.NET MVC Manually Accessing Request.Form & Potentially Dangerous values

查看:231
本文介绍了C#ASP.NET MVC手动访问Request.Form&潜在危险值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在序列化并保存表单,并针对每个用户请求将字符串数据查询到数据库中.该特定的已提交模型已经具有[AllowHtml]属性,并且可以向控制器精细提交.问题出在我记录请求的Global.asax文件中,当我访问此表单值时出现异常:

I'm serializing and saving form and query string data to a database for each user request. This particular submitted model already has the [AllowHtml] attribute and submits fine to the controller. The issue is inside the Global.asax file where I log the request, when I access this form value I get the exception:

"从 客户(...)."

"A potentially dangerous Request.Form value was detected from the client (...)."

protected void Application_PostRequestHandlerExecute(Object sender, EventArgs e)
{
    ...
    var serializer = new JavaScriptSerializer();
    var formData = (Request.Form.Count == 0) ? "" : serializer.Serialize(Request.Form.AllKeys.Where(x => x != null).ToDictionary(k => k, k => Request.Form[k]));
    ...
}

当尝试访问Request.Form [k]包含无效字符时,将引发错误.

Error gets thrown when it tries to access Request.Form[k] when it contains invalid characters.

推荐答案

使用Request.Form[]访问值将触发请求验证(因此为异常).您可以使用HttpRequestUnvalidated属性来获取请求值,而无需触发验证.

Accessing values with Request.Form[] will trigger request validation (hence the exception). You can use the Unvalidated property of HttpRequest to get the request values without triggering validation.

替换

Request.Form[k]

使用

Request.Unvalidated.Form[k]

谨慎使用-从

安全性注:如果使用此属性,则必须手动检查数据以防潜在的跨站点脚本攻击.

Security Note: If you use this property, you must manually check the data for potential cross-site scripting attacks.

这篇关于C#ASP.NET MVC手动访问Request.Form&潜在危险值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆