我正在尝试像示例中那样实现antiXSS http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/avoiding-cross-site-scripting-xss-attacks-with-antixss-in/ 任何人都可以弄清楚我在这里失踪了什么。它并没有像预期的那样发生。
到目前为止,我已经添加了antiXSS库。我可以从引用中确认它,所以添加了antiXsslibrary和HtmlSanitazationLibrary。在web配置中,我也添加了这个 httpruntime encoderType =" System.Web.Security.AntiXss.AntiXssEncoder,System.Web, Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a" (不允许复制粘贴确切的代码)
在控制器类中,我更新了以下内容。
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult Create([Bind(Include = "CourseID,Title,Credits,DepartmentID,Link")]Course course)
{
try
{
if (ModelState.IsValid)
{
course.Link = Sanitizer.GetSafeHtmlFragment(course.Link);
db.Courses.Add(course);
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (DbEntityValidationException ex /* dex */)
{
string er="";
//Log the error (uncomment dex variable name and add a line here to write a log.)
foreach (var entityValidationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in entityValidationErrors.ValidationErrors)
{
er+="Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage;
}
}
ModelState.AddModelError("", er);
}
PopulateDepartmentsDropDownList(course.DepartmentID);
return View(course);
}
Sanitizer.GetSafeHtmlFragment没有摆脱html代码。我错过了什么。