启用asp.net核心请求验证

Mar*_*tin 16 c# asp.net-core

我错过了什么或asp.net核心允许在用户文本字段中发布脚本标记?在asp.net mvc的早期版本中,我需要通过[AllowHtml]属性允许它.

有没有办法如何启用验证再次具有潜在危险的价值?

我可以随意提交价值

<script src='http://test.com/hack.js'></script>
Run Code Online (Sandbox Code Playgroud)

在表格发布期间.

模型:

using System.ComponentModel.DataAnnotations;

namespace Test.Models
{
    public class TestModel
    {
        [MaxLength(500)]
        public string Content { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

using Microsoft.AspNetCore.Mvc;
using Test.Models;

namespace Test.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            var model = new TestModel { Content = "Test" };
            return View();
        }

        [HttpPost]
        public IActionResult Index(TestModel model)
        {
            if(!ModelState.IsValid)
                return View(model);

            return Content("Success");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

视图:

@model TestModel

<form asp-action="Index" asp-controller="Home" method="post">
    <div asp-validation-summary="All"></div>
        <label asp-for="Content">Content<strong>*</strong></label>
        <span asp-validation-for="Content"></span>
        <input asp-for="Content" type="text" />
    </div>
</form>
Run Code Online (Sandbox Code Playgroud)

pet*_*ter 11

ASP.NET核心没有类似于Request验证的功能,正如微软认定的那样,这不是一个好主意.有关更多信息,请参阅有关ASP.NET核心问题的讨论' 用于请求验证默认中间件,如IIS有 '.

这意味着必须在入站模型上进行验证.在Razor(.cshtml)中你应该输出用户提供的输入@Model.Content,如编码给定的字符串.

请记住,当输出的文本不在Html部分内时,那些转义技术可能不起作用.

因此,@Html.Raw(..)除非您知道所提供的数据已经过消毒,否则请勿使用.

补充: