Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc AntiXss保护Html模型属性_Asp.net Mvc - Fatal编程技术网

Asp.net mvc AntiXss保护Html模型属性

Asp.net mvc AntiXss保护Html模型属性,asp.net-mvc,Asp.net Mvc,我的一些模型属性由AllowHtml属性标记。是否有任何方法可以自动对这些字段应用AntiXss保护(即仅过滤允许的标记)?没有自动方法。您所能做的最接近的事情就是获取AntiXss Nuget包。然后,您可以在控制器中使用它,如下所示: Microsoft.Security.Application.Sanitizer.GetSafeHtml("YourHtml"); 或 如果您使用,您可以使用 Server.HtmlDecode("HtmlEncodedString"); 希望这能

我的一些模型属性由AllowHtml属性标记。是否有任何方法可以自动对这些字段应用AntiXss保护(即仅过滤允许的标记)?

没有自动方法。您所能做的最接近的事情就是获取AntiXss Nuget包。然后,您可以在控制器中使用它,如下所示:

  Microsoft.Security.Application.Sanitizer.GetSafeHtml("YourHtml");

如果您使用,您可以使用

  Server.HtmlDecode("HtmlEncodedString");

希望这能有所帮助。

首先,好吧,没有什么内置的。 但是MVC允许通过定制的ModelBinder轻松地完成这些事情,您可以定义

public class CustomAntiXssAttribute : Attribute { }
并用它装饰您的财产(如果您愿意,甚至可以从
allowtmlatAttribute
继承)。然后,使用模型绑定器,您可以添加特定的防xss保护:

    public class CutstomModelBinder : DefaultModelBinder
    {
        protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
        {
            if (propertyDescriptor.Attributes.OfType<CustomAntiXssAttribute>().Any())
            {
                var valueResult = bindingContext.ValueProvider.GetValue(propertyDescriptor.Name);
                var filteredValue = SOME_CUSTOM_FILTER_FUNCTION_HERE(valueResult.AttemptedValue);
                propertyDescriptor.SetValue(bindingContext.Model, filteredValue);
            }
            else // revert to the default behavior.
            {
                base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
            }
        }
    }
公共类CutstomModelBinder:DefaultModelBinder
{
受保护的覆盖无效BindProperty(ControllerContext ControllerContext、ModelBindingContext bindingContext、System.ComponentModel.PropertyDescriptor PropertyDescriptor)
{
if(propertyDescriptor.Attributes.OfType().Any())
{
var valueResult=bindingContext.ValueProvider.GetValue(propertyDescriptor.Name);
var filteredValue=此处的一些自定义过滤器函数(valueResult.AttemptedValue);
propertyDescriptor.SetValue(bindingContext.Model,filteredValue);
}
else//恢复到默认行为。
{
BindProperty(controllerContext、bindingContext、propertyDescriptor);
}
}
}
然后在
这个
函数中,你可以使用@Yogiraj建议的,或者使用Regexp,甚至应用基于HtmlAgilityPack的过滤

注意:不要忘记添加
ModelBinders.Binders.DefaultBinder=new CutstomModelBinder()应用程序启动(我忘了:)

未测试的代码

public class ADefaultModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelMetadata.RequestValidationEnabled)
        {
            var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;
            value = value.Replace("&", "");// replace existing & from the value
            var encodedValue = Microsoft.Security.Application.Encoder.HtmlEncode(value);
            bindingContext.ModelMetadata.RequestValidationEnabled = encodedValue.Contains("&"); // Whether AntiXss encoded a char to &..
        }
        return base.BindModel(controllerContext, bindingContext);
    }
}
public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ModelBinders.Binders.DefaultBinder = new ADefaultModelBinder();

我会用
regularpression
数据注释验证替换那些
allowtml
属性。这样做的好处是,您可以捕获错误并向用户显示错误所在,而前者在全局级别触发错误

例如

public class MyViewModel
{
    [DataType(DataType.MultilineText)]
    [RegularExpression(@"^[^\<\>]*$", ErrorMessage = "May not contain <,>")]
    public string Text { get; set; }
}
公共类MyViewModel
{
[数据类型(DataType.multilitext)]
[正则表达式(@“^[^\]*$”,ErrorMessage=“可能不包含”)]
公共字符串文本{get;set;}
}

Ref:

您不能从ALLOWHTMLATTRIBUTE继承“它是一个密封类”
public class MyViewModel
{
    [DataType(DataType.MultilineText)]
    [RegularExpression(@"^[^\<\>]*$", ErrorMessage = "May not contain <,>")]
    public string Text { get; set; }
}