asp.net mvc 自定义模型绑定 有潜在的Requset.Form

自定义了一个模型绑定器。前端会传过来一些敏感字符。调用bindContext. valueProvider.GetValue(key) 则会报错 说 有潜在的从客户端中检测到有潜在危险的 Request.QueryString 值

百度谷歌都没有找到相关答案

MVC自带的默认绑定器(DefaultModelBinder)在action方法打上[ValidateInput(false)]或则在跳过验证的字段上打上[AllowHtml] 特性则可以跳过敏感字符验证

下载MVC4源码发现 他在获取的其中一个绑定方法

  1. public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  2. {
  3. EnsureStackHelper.EnsureStack();
  4.  
  5. if (bindingContext == null)
  6. {
  7. throw new ArgumentNullException("bindingContext");
  8. }
  9.  
  10. bool performedFallback = false;
  11.  
  12. if (!String.IsNullOrEmpty(bindingContext.ModelName) && !bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
  13. {
  14. // We couldn't find any entry that began with the prefix. If this is the top-level element, fall back
  15. // to the empty prefix.
  16. if (bindingContext.FallbackToEmptyPrefix)
  17. {
  18. bindingContext = new ModelBindingContext()
  19. {
  20. ModelMetadata = bindingContext.ModelMetadata,
  21. ModelState = bindingContext.ModelState,
  22. PropertyFilter = bindingContext.PropertyFilter,
  23. ValueProvider = bindingContext.ValueProvider
  24. };
  25. performedFallback = true;
  26. }
  27. else
  28. {
  29. return null;
  30. }
  31. }
  32.  
  33. // Simple model = int, string, etc.; determined by calling TypeConverter.CanConvertFrom(typeof(string))
  34. // or by seeing if a value in the request exactly matches the name of the model we're binding.
  35. // Complex type = everything else.
  36. if (!performedFallback)
  37. {
  38. bool performRequestValidation = ShouldPerformRequestValidation(controllerContext, bindingContext);
  39. ValueProviderResult valueProviderResult = bindingContext.UnvalidatedValueProvider.GetValue(bindingContext.ModelName, skipValidation: !performRequestValidation);
  40. if (valueProviderResult != null)
  41. {
  42. return BindSimpleModel(controllerContext, bindingContext, valueProviderResult);
  43. }
  44. }
  45. if (!bindingContext.ModelMetadata.IsComplexType)
  46. {
  47. return null;
  48. }
  49.  
  50. return BindComplexModel(controllerContext, bindingContext);
  51. }

红色处发现他在获取参数之前获得一个bool值,

点开看看这个方法

  1. private static bool ShouldPerformRequestValidation(ControllerContext controllerContext, ModelBindingContext bindingContext)
  2. {
  3. if (controllerContext == null || controllerContext.Controller == null || bindingContext == null || bindingContext.ModelMetadata == null)
  4. {
  5. // To make unit testing easier, if the caller hasn't specified enough contextual information we just default
  6. // to always pulling the data from a collection that goes through request validation.
  7. return true;
  8. }
  9.  
  10. // We should perform request validation only if both the controller and the model ask for it. This is the
  11. // default behavior for both. If either the controller (via [ValidateInput(false)]) or the model (via [AllowHtml])
  12. // opts out, we don't validate.
  13. return (controllerContext.Controller.ValidateRequest && bindingContext.ModelMetadata.RequestValidationEnabled);
  14. }

//看注释好像发现了什么 让我们通过打特性 来跳过验证

.发现他的getValue 有个重载 如果传入true则可以跳过验证

高兴的回去改自己的代码 发现bindContext.valueProvider的值提取器是接口类型System.Web.Mvc.ModelBindingContext.ValueProvider   他是没有重载方法的

看看这个东东ValueProviderResult valueProviderResult = bindingContext.UnvalidatedValueProvider.GetValue(bindingContext.ModelName, skipValidation: !performRequestValidation); 的UnvalidatedValueProvider到底是什么

  1. public IValueProvider ValueProvider { get; set; }
  2.  
  3. internal IUnvalidatedValueProvider UnvalidatedValueProvider
  4. {
  5. get { return (ValueProvider as IUnvalidatedValueProvider) ?? new UnvalidatedValueProviderWrapper(ValueProvider); }
  6. }

看到这里 回头修改自己的代码吧

  1. //如果是基本类型 直接在值提供器中拿值绑定
  2. if (property.PropertyType.IsValueType || property.PropertyType == typeof (string))
  3. {
  4. var newkey = property.Name;
  5. if (Regex.IsMatch(key, @"\[\w*\]"))
  6. {
  7. newkey = key + "[" + newkey + "]";
  8. }
  9.  
  10. var value = bindingContext.ValueProvider.GetValue(newkey);
  11. if (value == null)
  12. {
  13.  
  14. //再尝试获取一次
  15. var valueProvider = (bindingContext.ValueProvider as IUnvalidatedValueProvider);
  16. newkey = key + "[" + newkey + "]";
  17. value = valueProvider.GetValue(newkey,true);
  18. }

当然这么做还是不严谨的 我们需要也像defualtModelBind一样 通过特性来标示是否跳过验证。而不是所有使用模型绑定的action请求都跳过验证

asp.net MVC 自定义模型绑定 从客户端中检测到有潜在危险的 Request.QueryString 值的更多相关文章

  1. MVC去掉传参时的验证:从客户端中检测到有潜在危险的Request.QueryString值

    解决方法:给Action添加属性[ValidateInput(false)]. 例: [ValidateInput(false)] public ActionResult Index(string o ...

  2. 从客户端中检测到有潜在危险的 Request.QueryString 值

    解决办法: 一.解决方法是在web.config的 里面加入<system.web> <pages validateRequest="false"/>< ...

  3. 从客户端中检测到有潜在危险的 request.form值 以及 request.querystring[解决方法]

    一.从客户端中检测到有潜在危险的request.form值 当页面编辑或运行提交时,出现“从客户端中检测到有潜在危险的request.form值”问题,该怎么办呢?如下图所示: 下面博主汇总出现这种错 ...

  4. ASP.NET MVC从客户端中检测到有潜在危险的 Request.Form 值

    ASP.NET MVC4(Razor)从客户端中检测到有潜在危险的 Request.Form 值 “/”应用程序中的服务器错误. 从客户端(Content=" sdfdddd ...&quo ...

  5. MVC中提示错误:从客户端中检测到有潜在危险的 Request.Form 值的详细解决方法

    今天往MVC中加入了一个富文本编辑框,在提交信息的时候报了如下的错误:从客户端(Content="<EM ><STRONG ><U >这是测试这...&q ...

  6. ASP.NET MVC4(Razor)从客户端中检测到有潜在危险的 Request.Form 值

    SP.NET MVC4(Razor)从客户端中检测到有潜在危险的 Request.Form 值 “/”应用程序中的服务器错误. 从客户端(Content=" sdfdddd ..." ...

  7. 【异常记录(七)】MVC:从客户端中检测到有潜在危险的 Request.Form 值 的解决方法 [转]

    从客户端(Content="<EM ><STRONG ><U >这是测试这...")中检测到有潜在危险的Request.Form 值. 说明:  ...

  8. mvc 从客户端 中检测到有潜在危险的 Request.Form 值

    天往MVC中加入了一个富文本编辑框,在提交信息的时候报了如下的错误:从客户端(Content="<EM ><STRONG ><U >这是测试这...&qu ...

  9. [转]从客户端中检测到有潜在危险的 Request.Form 值。

    参考资料: ASP.NET 4.0中使用FreeTextBox和FCKeditor遇到安全问题警告的解决办法关于问题出现的原因说的很清楚 引言 本人在.NET 4.0+VS2010环境下调试一个ASP ...

随机推荐

  1. Aizu/Aoj 0033 Ball

    题目大意: 有编号1到10共10个球,从上方丢下去,入口处可以选择进入左边或者右边,最后10个球全部落下去后如果左右两侧都是从小到大的顺序,则输出YES:否则输出NO. 题目原本的标签枚举,复杂度是2 ...

  2. Python学习笔记24:Django搭建简单的博客站点(二)

    上一节说道怎样使用Django创建并执行一个项目.这节说怎样加入一个博客应用. 一 项目跟应用的关系 在加入应用之前,先来看看项目与应用之间有什么不同之处呢? 项目是针对一个特定的 Web 站点相关的 ...

  3. ios swift学习日记4-字符串和字符

    近期ios的swift语言好像火了起来,本人没有objectc的基础,但之前是有c跟java的基础的. 从这几天開始学习ios的swift语言,后期以博客形式公布.这里提供一本翻译的英文版的swif书 ...

  4. 深度学习将会变革NLP中的中文分词——TODO 待好好细看

    见:https://www.leiphone.com/news/201608/IWvc75oJglAIsDvJ.html TODO 待好好细看

  5. poj--2239--Selecting Courses(最大匹配)

    Selecting Courses Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9782   Accepted: 4400 ...

  6. 【概率证明】—— sum and product rules of probability

    1. sum and product rules of probability ⎧⎩⎨p(x)=∫p(x,y)dyp(x,y)=p(x|y)p(y) sum rule of probability 的 ...

  7. 05.使用jdk发布webservice服务

    无论服务端是用什么写的,使用框架写的还是用jdk写的,它都会发布出来这样一个东西.主要你遵循咱们这七个步骤来走就可以调用了. 咱们现在转换一下角色,自己发布一个服务让别人去调.怎么来发布一个服务? 我 ...

  8. TensorFlow——分布式的TensorFlow运行环境

    当我们在大型的数据集上面进行深度学习的训练时,往往需要大量的运行资源,而且还要花费大量时间才能完成训练. 1.分布式TensorFlow的角色与原理 在分布式的TensorFlow中的角色分配如下: ...

  9. Excel数据迁移到SQL Server遇到的若干问题

    系统环境为:Windows Server 2008 r2 SQL Server 2012 1.建表过程中,如果用图形化的方式修改表结构会遇到问题: '不允许保存更改.您所做的更改要求删除并重新创建以下 ...

  10. Unity3d Vector3

    using UnityEngine; using System.Collections; public class test : MonoBehaviour { void Start () { Vec ...