ModelBiner不验证某个属性

问题

使用MVC的同学十有八九都会遇到这个错误:从客户端(Content="<script>...")中检测到有潜在危险的Request.Form 值。

这个错误是在请求中,用户输入的内容包含一些特殊字符而导致的,这是MVC为防止XSS而考虑的。

比如我们需要在后台发布一篇新闻,新闻的内容是富文本编辑器编辑得到HTML,那么在请求到Action的时候,默认的就会出现这个错误。

方案1

解决这个错误通常的做法是,在处理请求的Action上增加特性:[ValidateInput(false)]

方案2

方案一的做法有些过于大方了,该特性允许使得本次请求的所有参数都放弃了验证。

还是以发布新闻为例,需要提交标题和内容,那么即使标题中包含特殊字符,也会忽略验证。显然,这不是我们想要的。

我们其实想要的是,只允许新闻内容属性不验证,其他的属性默认还是要验证。

比如,我们给不需要验证的属性,增加一个[xxx]特性。

事实上,MVC实现了这样一个特性:[AllowHtml]

详细见:https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.allowhtmlattribute?view=aspnet-mvc-5.2

方案3

方案二基本满足了我们的需求,但是有一点,该特性在MVC命名空间下定义。往往我们的Model类是独立的类库,如果使用该特性,还需要引入MVC dll,感觉不是很好。

那么我们自己实现这样一个特性,标记在指定的属性上,在ModelBinder绑定属性的时候,包含该特性的属性,指定其不验证。

1.首先在global.asax中指定新的DefaultBinder

ModelBinders.Binders.DefaultBinder = new MyModelBinder();

2.定义MyAtrr

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class MyAttribute : Attribute
{

}

3.实现MyModelBinder

public class MyModelBinder : DefaultModelBinder
{
  protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
  {
       bool containMyAttr = propertyDescriptor.Attributes.OfType<MyAttribute>().Any();
       if (containMyAtrr)
       {
            bindingContext.ModelMetadata.RequestValidationEnabled = false;
       }
       return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
  }
}
原文地址:https://www.cnblogs.com/talentzemin/p/10638620.html