c# - 如何防止 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();
}
解决方案
在 .cshtml 文件中也使用 @Html.AntiForgeryToken()。
@using (Html.BeginForm(FormMethod.Post))
{
@Html.AntiForgeryToken()
}
推荐阅读
- ionic-framework - Ionic 4 ion-slides 使用 ngClass 更改活动幻灯片的 css 样式
- java - Java Rest 服务 - 响应接受并在后面处理请求
- allure - 为什么诱惑报告中只显示少数结果
- javascript - 深度替换一个知道我们必须注入值的对象中的属性值的值
- android - 如何在房间交易Android上完成退货
- angular - 键入“可观察<{} | Employee>' 不可分配给类型 'Observable
' - string - 使用 c# 将十六进制字符串转换为 int
- java - AAPT2 错误:检查日志以获取详细信息(在调试版本上工作,但在发布版本上失败)
- javascript - 等待 *ngFor 构建页面以执行 DOM 操作
- javascript - React - 未捕获的 ReferenceError:未定义添加