【发布时间】:2018-08-30 13:35:30
【问题描述】:
我想从我的 ASP.Net MVC 5 网站中删除一个产品。我想知道添加[AntiForgeryToken] 和[Authorize] 是否足以保护删除操作?
查看
<p>Delete: @Model.Name</p>
@using (Html.BeginForm("Delete", "ProductController", FormMethod.Post, new { ProductId = Model.ProductId }))
{
@Html.AntiForgeryToken()
<button type="submit">Delete</button>
}
控制器
[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult Delete(long ProductId)
{
/* Do I need to check if the logged in User has permission to delete the product?
var product = ProductRepository.Get(Id);
if (product.Creator == User.Identity.GetUserId<long>())
{
ProductRepository.Delete(ProductId);
}
*/
// or, can I avoid the trip to DB and just delete the record?
ProductRepository.Delete(ProductId);
}
场景:黑客在我的网站上注册并创建了一个有效帐户。现在黑客查看了他自己的产品,显然他有一个 AntiForgeryToken。他现在可以在浏览器中更改 ProductId 并发布删除其他人产品的请求吗?
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-5 antiforgerytoken