我的一个 View 中有一个网格,并在其中创建了一个“操作”列。该列中有用于编辑和删除的按钮。当用户单击删除按钮时,我使用 jQuery 来访问其单击事件。
这是我的 ajax 删除的样子:
$(document).on('click', '.btnDeleteRole', function (e) {
e.preventDefault();
if (confirm("Are you sure you want to delete this record?")) {
var $this = $(this); //store $(this) to a variable
var roleId = $this.attr('data-role-Id');
$.ajax({
type: "POST",
url: "/admin/roles/delete",
data: { id: roleId },
dataType: "html",
success: function (data) {
// rebind kendo grid
}
});
}
});
这是我的删除操作方法:
// POST: Roles/Delete/5
[Route("roles/delete")]
[HttpPost]
public async Task<IActionResult> RoleDelete(string id)
{
IdentityRole role = await _roleManager.FindByIdAsync(id);
await _roleManager.DeleteAsync(role);
return RedirectToAction("RoleIndex");
}
由于删除按钮不是表单的一部分,我无法在 ActionMethod 上方添加 AntiForgeryToken 注释。
删除这样的项目安全吗?
最佳答案
不,这不安全。如果您不使用 antiforgerytoken,您将面临 CSRF 攻击,如 here 中所述。 .
您只需在网格删除帖子中发送主表单隐藏 token 即可。您不需要每行一个 token ,因为您拥有表单的隐藏 token 和 cookie 的 token ,这就是您所需要的。
关于c# - 在 ASP.NET MVC 中,没有 AntiForgeryToken 的删除操作方法不安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36682154/