
首先,要准备一份白名单的配置,使用XML作为配置形式,如下:
<?xml version="1.0" encoding="utf-8" ?>
<config>
<tag name="*">
<attr name="style">^((font|color)\s*:\s*[^]+\s*)*$</attr>
</tag>
<tag name="a">
<attr name="href">^[a-zA-Z]+://.+$</attr>
<attr name="title">.+</attr>
</tag>
...
</config>
根元素下的每个tag元素表示一个合法的HTML元素,其中星号表示对所有元素的统一设置,例如上面的配置便开放了所有元素的style属性中的font和color两个设置,以及a元素中的href和title属性,其中href还必须是“{scheme}://xxx”的形式,这样就避免了“javascript:alert(1)”这样的XSS问题。
其次写filter进行过滤:
public class FilterConfig : Dictionary<string, TagConfig>
{
public FilterConfig()
: base(StringComparer.OrdinalIgnoreCase)
{ }
public FilterConfig(XElement config)
{
var wildcardElement = config
.Elements("tag")
.SingleOrDefault(e =>e.Attribute("name").Value == "*")
var wildcardConfig = wildcardElement == null ? null :
new TagConfig(wildcardElement)
foreach (var ele in config
.Elements("tag")
.Where(e =>e.Attribute("name").Value != "*"))
{
var name = ele.Attribute("name").Value
var tagConfig = new TagConfig(ele)
foreach (var pair in wildcardConfig)
{
if (!tagConfig.ContainsKey(pair.Key))
{
tagConfig.Add(pair.Key, pair.Value)
}
}
this.Add(name, tagConfig)
}
}
}
你可以试试
[Bind(Exclude = "l_content")] 是说页面上不要绑它,就算你也不更新它,但是这些都不影响它的属性[Required]。你不用它并不代表它不存在了,就像“闭上眼睛就是天黑”,其实天并没有黑。解决的办法就是给个临时值了,比如“TempValue”,反正跳过验证就行了。有问题hi我。这个默认验证是在web.config配置文件中设置的<add
key="ClientValidationEnabled"
value="true"/>设置为false就行了,
这个是去掉所有model的默认验证,然后你可以在model中加上自己的验证,这样哪些属性需要验证,哪些不需要
就看你自己了
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)