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

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

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

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

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

 public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            EnsureStackHelper.EnsureStack();

            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }

            bool performedFallback = false;

            if (!String.IsNullOrEmpty(bindingContext.ModelName) && !bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
            {
                // We couldn't find any entry that began with the prefix. If this is the top-level element, fall back
                // to the empty prefix.
                if (bindingContext.FallbackToEmptyPrefix)
                {
                    bindingContext = new ModelBindingContext()
                    {
                        ModelMetadata = bindingContext.ModelMetadata,
                        ModelState = bindingContext.ModelState,
                        PropertyFilter = bindingContext.PropertyFilter,
                        ValueProvider = bindingContext.ValueProvider
                    };
                    performedFallback = true;
                }
                else
                {
                    return null;
                }
            }

            // Simple model = int, string, etc.; determined by calling TypeConverter.CanConvertFrom(typeof(string))
            // or by seeing if a value in the request exactly matches the name of the model we're binding.
            // Complex type = everything else.
            if (!performedFallback)
            {
                bool performRequestValidation = ShouldPerformRequestValidation(controllerContext, bindingContext);
                ValueProviderResult valueProviderResult = bindingContext.UnvalidatedValueProvider.GetValue(bindingContext.ModelName, skipValidation: !performRequestValidation);
                if (valueProviderResult != null)
                {
                    return BindSimpleModel(controllerContext, bindingContext, valueProviderResult);
                }
            }
            if (!bindingContext.ModelMetadata.IsComplexType)
            {
                return null;
            }

            return BindComplexModel(controllerContext, bindingContext);
        }

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

点开看看这个方法

     private static bool ShouldPerformRequestValidation(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (controllerContext == null || controllerContext.Controller == null || bindingContext == null || bindingContext.ModelMetadata == null)
            {
                // To make unit testing easier, if the caller hasn't specified enough contextual information we just default
                // to always pulling the data from a collection that goes through request validation.
                return true;
            }

            // We should perform request validation only if both the controller and the model ask for it. This is the
            // default behavior for both. If either the controller (via [ValidateInput(false)]) or the model (via [AllowHtml])
            // opts out, we don't validate.
            return (controllerContext.Controller.ValidateRequest && bindingContext.ModelMetadata.RequestValidationEnabled);
        }

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

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

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

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

       public IValueProvider ValueProvider { get; set; }

        internal IUnvalidatedValueProvider UnvalidatedValueProvider
        {
            get { return (ValueProvider as IUnvalidatedValueProvider) ?? new UnvalidatedValueProviderWrapper(ValueProvider); }
        }

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

  //如果是基本类型 直接在值提供器中拿值绑定
                            if (property.PropertyType.IsValueType || property.PropertyType == typeof (string))
                            {
                                var newkey = property.Name;
                                if (Regex.IsMatch(key, @"\[\w*\]"))
                                {
                                    newkey = key + "[" + newkey + "]";
                                }

                                var value = bindingContext.ValueProvider.GetValue(newkey);
                                if (value == null)
                                {

                                    //再尝试获取一次
                                    var valueProvider = (bindingContext.ValueProvider as IUnvalidatedValueProvider);
                                    newkey = key + "[" + newkey + "]";
                                    value = valueProvider.GetValue(newkey,true);
                                }

当然这么做还是不严谨的 我们需要也像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. java 源代码的魅力

    学习一种语言: 最快的方法.就是研究其源码. 从源码中可以体会到各种经典的思想! 赞赏一下: 比如: 我们在写一些 冒泡和选择排序的时候用的 交换:     /**      * Swaps x[a] ...

  2. 驱动开发(8)处理设备I/O控制函数DeviceIoControl

    本博文由CSDN博主zuishikonghuan所作,版权归zuishikonghuan全部,转载请注明出处:http://blog.csdn.net/zuishikonghuan/article/d ...

  3. DeepLearning to digit recognizer in kaggle

    DeepLearning to digit recongnizer in kaggle 近期在看deeplearning,于是就找了kaggle上字符识别进行练习.这里我主要用两种工具箱进行求解.并比 ...

  4. Linux VGA驱动移植实验【转】

    本文转载自:http://m.blog.csdn.net/bzw73/article/details/46564275 有了前面的LCD驱动的框架,再移植VGA驱动就相当的容易了.默认在光盘中已经支持 ...

  5. 委托delegate,Action,Func,Predicate

    C#委托的介绍(delegate.Action.Func.predicate) 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 ...

  6. mybatis学习笔记之基础复习(3)

    mybatis学习笔记之基础复习(3) mybatis是什么? mybatis是一个持久层框架,mybatis是一个不完全的ORM框架.sql语句需要程序员自己编写, 但是mybatis也是有映射(输 ...

  7. python爬虫:读取PDF

    下面的代码可以实现用python读取PDF,包括读取本地和网络上的PDF. pdfminer下载地址:https://pypi.python.org/packages/source/p/pdfmine ...

  8. WinAPI使用: 时间,线程,中断

    (1):C/C++获取当前系统时间:http://www.cnblogs.com/mfryf/archive/2012/02/13/2349360.html 不过当计算算法耗时的时候,不要忘记seco ...

  9. DDoS攻击与防范策略

    DDoS(Distributed Denial of Service,分布式拒绝服务)攻击的主要目的是让指定目标无法提供正常服务,甚至从互联网上消失,是目前最强大.最难防御的攻击之一. 按照发起的方式 ...

  10. 考考你对java多态的理解

    请看如下代码, 如果你能不运行得出正确答案, 那你真的超过99%的java程序员了. [本人属于最大头的那部分] public class A{ public String s = "A&q ...