我已经在我的应用程序中实现了对我在互联网上的一些博客文章中阅读的信息后的CSRF攻击的缓解.特别是这些帖子是我实施的驱动力
基本上这些文章和建议说,为了防止CSRF攻击,任何人都应该实现以下代码:
1)添加[ValidateAntiForgeryToken]接受POST Http动词的每个动作
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SomeAction( SomeModel model ) {
}
Run Code Online (Sandbox Code Playgroud)
2)在<%= Html.AntiForgeryToken() %>向服务器提交数据的表单中添加帮助程序
<div style="text-align:right; padding: 8px;">
<%= Html.AntiForgeryToken() %>
<input type="submit" id="btnSave" value="Save" />
</div>
Run Code Online (Sandbox Code Playgroud)
无论如何,在我的应用程序的某些部分,我正在使用jQuery对服务器进行Ajax POST,而根本没有任何形式.例如,当我让用户点击图像来执行特定操作时,会发生这种情况.
假设我有一个包含活动列表的表.我在表的一列上有一个图像,上面写着"将活动标记为已完成",当用户点击该活动时,我正在进行Ajax POST,如下例所示:
$("a.markAsDone").click(function (event) {
event.preventDefault();
$.ajax({
type: "post",
dataType: "html",
url: $(this).attr("rel"),
data: {},
success: function (response) {
// ....
}
});
});
Run Code Online (Sandbox Code Playgroud)
我如何<%= Html.AntiForgeryToken() %>在这些情况下使用?我应该在Ajax调用的data参数中包含帮助器调用吗? …
我想保护一个ASP.NET Web应用程序来防止黑客攻击.是否有特定编码的ASP.NET特定任务列表,以使ASP.NET更安全?超越MSDN上提到的内容.我对有关避免跨站点请求伪造和跨站点脚本的方法的代码示例的特定步骤感兴趣.
我知道在连接到SQL Server时使用SQL参数进行SQL注入,Windows身份验证以及验证服务器上的表单输入.
这个美丽的抽象让你放在@Html.AntiForgeryToken()cshtml文件中,这个文件被神奇地扩展到类似的东西;
<input name="__RequestVerificationToken" type="hidden" value="JjMHm5KJQ/qJsyC4sgifQWWX/WmADmNvEgHZXXuB07bWoL84DrmQzE6k9irVyFSJ5VSYqeUIXgl4Dw4NHSotLwflGYTyECzLvrgzbtonxJ9m3GVPgUV7Z6s2Ih/klUB78GN7Fl4Gj7kxg62MEoGcZw175eVwTmkKJ0XrtEfD5KCVvYIMHNY8MT2l+qhltsGL87c9dII42AVoUUQ2gTvfPg==" />
Run Code Online (Sandbox Code Playgroud)
在页面提供之前通过mvc.但是我的页面有一些JavaScript进行ajax调用,即使它被添加到表单中也不包括令牌.他们目前正在获得预期,[HttpAntiForgeryException]: A required anti-forgery token was not supplied or was invalid.因为他们没有令牌.我知道我可以解析DOM中的值,但我不应该这样做.是否有其他方式来获取/获取此值?要清楚,我的意思是我想要一个方法的重载,它只返回值作为字符串或某种具有名称和值的对象.
为了提供更多的上下文我的表单和相关的JS看起来有点像这样;
<form action="/settings" method="post"><input name="__RequestVerificationToken" type="hidden" value="JjMHm5KJQ/qJsyC4sgifQWWX/WmADmNvEgHZXXuB07bWoL84DrmQzE6k9irVyFSJ5VSYqeUIXgl4Dw4NHSotLwflGYTyECzLvrgzbtonxJ9m3GVPgUV7Z6s2Ih/klUB78GN7Fl4Gj7kxg62MEoGcZw175eVwTmkKJ0XrtEfD5KCVvYIMHNY8MT2l+qhltsGL87c9dII42AVoUUQ2gTvfPg==" /> <fieldset>
<h3>User Settings</h3>
<ul>
<li>
label for="password">Password</label>
<a href="#" id="change_password" class="changePasswordButton">Edit</a>
<div id="password_section" class="inlineedit">
<div>
<span for="existing_password">Current password</span> <input autocomplete="off" class="required" id="existing_password" name="existing_password" type="password" />
</div>
<div>
<span for="new_password">New password</span> <input autocomplete="off" class="required" id="new_password" name="new_password" type="password" />
<span id="password_strength" />
</div>
<div>
<span for="confirm_password">Confirm password</span> <input autocomplete="off" class="required" id="confirm_password" …Run Code Online (Sandbox Code Playgroud) 我的ASP.NET核心代码中有以下模型:
public class TestItem
{
public string Id { get; set; }
public string Name { get; set; }
public List<TestSubItem> SubItems { get; set; }
}
public class TestSubItem
{
public string Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和我的Controllers/TestController.cs中的以下方法:
// POST: Test/Create
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create([FromBody]TestItem item)
{
// Use item here...
return View(item);
}
Run Code Online (Sandbox Code Playgroud)
我的页面中有以下JavaScript:
var mySubItems = [{"Id": "1"}];
function submitForm() {
// myForm contains an Id textbox and a Name textbox
$("#myForm").submit();
};
function doPost() …Run Code Online (Sandbox Code Playgroud) ajax ×2
asp.net-mvc ×2
csrf ×2
.net ×1
asp.net ×1
asp.net-core ×1
c# ×1
deployment ×1
json ×1
security ×1