[英]ASP MVC 4.5 AntiXSS Library + allow HTML content
ASP.NET 4.5和Visual Studio 2012的新增功能顯示了內置的AntiXSS庫,
<httpRuntime ...
encoderType="System.Web.Security.AntiXss.AntiXssEncoder,System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
@Html.TextBoxFor(x => x.Name, new { @class = "testClass", maxlength = "50" })
功能強大,您會得到
"A potentially dangerous Request.Form value was detected from the client (Name=\"<b> test </b>\").""
對於任何潛在危險的檢測,
但
如果我想要這種或保護,但又允許所見即所得html編輯器允許一些HTML內容,該怎么辦? (例如論壇帖子)
正如@NickBork在他的評論中提到的那樣,此類錯誤來自ASP.NET請求驗證,並且與AntyXSS庫無關。 AntiXSS庫不能保護您的應用程序免受危險輸入。 它可以為您提供幫助,但是您必須明確使用它。
要跳過對某些屬性的請求驗證,可以使用AllowHtmlAttribute :
// model
public class MyModel
{
[AllowHtml]
public string HtmlContent { get; set; }
}
// controller
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(MyModel myModel)
{
// use myModel.HtmlContent
return View(myModel);
}
}
@* view *@
@model MyModel
<form action="@Url.Action("Index")" method="POST">
@Html.TextBoxFor(m => m.HtmlContent)
<button type="submit">Submit</button>
</form>
@if (Model != null)
{
<div>
@Html.Raw(Model.HtmlContent)
</div>
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.