首页 > 解决方案 > 如何防止 MVC Web 应用程序上的跨站点脚本

问题描述

我已经申请了防伪令牌和 Google Re-captcha。但是我们需要防止跨站点脚本,如果有其他人认为请告诉我。

例子:

[HttpPost, ValidateAntiForgeryToken, AllowAnonymous]
public ActionResult Demo(Register model)
 {
            if (ModelState.IsValid)
            {
                var response = Request["g-recaptcha-response"];
                var captchaReponse = ValidateCaptcha(response);
                if (captchaReponse.Item1)
                {
                    //Execute code as requirement
                }
            }
            return View();
        }

标签: c#asp.net-mvc

解决方案


在 .cshtml 文件中也使用 @Html.AntiForgeryToken()。

 @using (Html.BeginForm(FormMethod.Post))
{
        @Html.AntiForgeryToken()
}


推荐阅读