# XSS(Cross-Site Scripting)

预防措施:

  • 前端:Razor默认开启了HTML Encoding 但是可以手动关闭:@Html.Raw()
  • 后端:
//注入
private readonly HtmlEncoder _htmlEncoder
public MyConller(HtmlEncoder htmlEncoder){
	_htmlEncoder=htmlEncoder
}
//使用
_htmlEncoder.Encode(model.content);

# CSRF(Cross-Site Request Forgery)

预防措施:

  • 单个Action:[ValidateAntiForgeryToken]
  • 全局:
//ConfigueServices
services.AddAntiforgery(options =>
{
	options.FormFieldName = "AntiforgeryFieldname";
	options.HeaderName = "X-CRSF-TOKEN-HEADERNAME";
	options.SuppressXFrameOptionsHeader = false;
});
services.AddMvc(options=>{
	options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
})

//单个不加
[IgnoreAntiforgeryToken]