ASP.NET MVC - 默认情况下对所有字符串进行 HtmlEncode
ASP.NET MVC - HtmlEncode all strings by default
例如,用户在论坛上创建了一个新问题。
我将ajax发送到服务器,然后我使用HtmlEncode排除HTML代码,然后将其保存到数据库中。
是否可以在接收请求时自动使用HtmlEncode?
此外,当使用属性(例如 [HtmlAllowed])时,您可以在请求中允许 html 代码。
谢谢
您可以使用自定义模型绑定器实现它,当ASP.NET尝试将请求绑定到操作方法
的参数时,每个字符串属性或字符串参数都将通过此方法
public class StringBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
ValueProviderResult value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, value);
var str = (string)value?.ConvertTo(typeof(string));
str = HttpUtility.HtmlEncode(str);
return str;
}
}
并且在Application_Start()
ModelBinders.Binders.Add(typeof(string), new StringBinder());
感谢 Yegor Androsov 为我指明了正确的方向。
此 ModelBinder 自动编码所有字符串属性,除了具有 [SafeHtml]
属性
public class SafeStringModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
HttpRequestBase request = controllerContext.HttpContext.Request;
string name = bindingContext.ModelName;
string value = request.Unvalidated[name];
Type holderType = bindingContext.ModelMetadata.ContainerType;
if (holderType != null)
{
PropertyInfo propertyType = holderType.GetProperty(bindingContext.ModelMetadata.PropertyName);
if (propertyType == null) return value;
object[] attributes = propertyType.GetCustomAttributes(true);
bool hasAttribute = attributes.Cast<Attribute>().Any(a => a.GetType() == typeof (SafeHtmlAttribute));
if (!hasAttribute && !string.IsNullOrEmpty(value))
{
value = HttpUtility.HtmlEncode(value);
}
}
return value;
}
}
[AttributeUsage(AttributeTargets.Property)]
public class SafeHtmlAttribute : Attribute { }
例如,用户在论坛上创建了一个新问题。
我将ajax发送到服务器,然后我使用HtmlEncode排除HTML代码,然后将其保存到数据库中。
是否可以在接收请求时自动使用HtmlEncode?
此外,当使用属性(例如 [HtmlAllowed])时,您可以在请求中允许 html 代码。
谢谢
您可以使用自定义模型绑定器实现它,当ASP.NET尝试将请求绑定到操作方法
的参数时,每个字符串属性或字符串参数都将通过此方法public class StringBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
ValueProviderResult value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, value);
var str = (string)value?.ConvertTo(typeof(string));
str = HttpUtility.HtmlEncode(str);
return str;
}
}
并且在Application_Start()
ModelBinders.Binders.Add(typeof(string), new StringBinder());
感谢 Yegor Androsov 为我指明了正确的方向。
此 ModelBinder 自动编码所有字符串属性,除了具有 [SafeHtml]
属性
public class SafeStringModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
HttpRequestBase request = controllerContext.HttpContext.Request;
string name = bindingContext.ModelName;
string value = request.Unvalidated[name];
Type holderType = bindingContext.ModelMetadata.ContainerType;
if (holderType != null)
{
PropertyInfo propertyType = holderType.GetProperty(bindingContext.ModelMetadata.PropertyName);
if (propertyType == null) return value;
object[] attributes = propertyType.GetCustomAttributes(true);
bool hasAttribute = attributes.Cast<Attribute>().Any(a => a.GetType() == typeof (SafeHtmlAttribute));
if (!hasAttribute && !string.IsNullOrEmpty(value))
{
value = HttpUtility.HtmlEncode(value);
}
}
return value;
}
}
[AttributeUsage(AttributeTargets.Property)]
public class SafeHtmlAttribute : Attribute { }