This delete button will only call the controller method if I remove the ValidateAntiForgeryToken
attribute. How can I change either of these methods to keep the antiforgery token. I am assuming .NET does not believe this way of deleting is safe and causes the AJAX to not hit the controller. I would like to find the most secure way possible to send a delete call.
Delete button AJAX.
$("table").on("click", ".btn-delete", function (e) {
var thisId = $(this).data('id');
if (confirm("Are you sure you want to delete this item?")) {
$.post('/AdminPortal/Client/Delete/', { id: thisId }, function (data) {
window.location.href = "/AdminPortal/Client/Index";
});
}
});
Delete Method Controller
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Delete(string id)
{
//add priv check here in the future
var client = baseSvc.ClientRepo.GetById(long.Parse(id));
if (client != null)
{
client.IsActive = false;
baseSvc.ClientRepo.Save(client);
}
return RedirectToAction(nameof(Index), new { area = nameof(AdminPortal) });
}