How to include the @Html.AntiForgeryToken() when deleting an object using a Delete link
Asked Answered
A

2

13

i have the following ajax.actionlink which calls a Delete action method for deleting an object:-

 @if (!item.IsAlreadyAssigned(item.LabTestID))
        { 
        string i = "Are You sure You want to delete (" + @item.Description.ToString() + ") ?";
           @Ajax.ActionLink("Delete",
       "Delete", "LabTest",
      new { id = item.LabTestID },

new AjaxOptions
{ Confirm = i,
    HttpMethod = "Post",
    OnSuccess = "deletionconfirmation",
    OnFailure = "deletionerror"
})
} 

but is there a way to include @Html.AntiForgeryToken() with the Ajax.actionlink deletion call to make sure that no attacker can send a false deletion request?

BR

Abshire answered 23/4, 2012 at 0:23 Comment(0)
A
17

You need to use the Html.AntiForgeryToken helper which sets a cookie and emits a hidden field with the same value. When sending the AJAX request you need to add this value to the POST data as well.

So I would use a normal link instead of an Ajax link:

@Html.ActionLink(
    "Delete", 
    "Delete", 
    "LabTest", 
    new { 
        id = item.LabTestID
    }, 
    new { 
        @class = "delete",
        data_confirm = "Are You sure You want to delete (" + item.Description.ToString() + ") ?"
    }
)

and then put the hidden field somewhere in the DOM (for example before the closing body tag):

@Html.AntiForgeryToken()

and finally unobtrusively AJAXify the delete anchor:

$(function () {
    $('.delete').click(function () {
        if (!confirm($(this).data('confirm'))) {
            return false;
        }

        var token = $(':input:hidden[name*="RequestVerificationToken"]');
        var data = { };
        data[token.attr('name')] = token.val();
        $.ajax({
            url: this.href,
            type: 'POST',
            data: data,
            success: function (result) {

            },
            error: function () {

            }
        });

        return false;
    });
});

Now you could decorate your Delete action with the ValidateAntiForgeryToken attribute:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(int id)
{
    ...
}
Apeman answered 23/4, 2012 at 7:32 Comment(2)
thanks for the reply ,, so there is not way to define this in the original Ajax.Actionlink?.Abshire
And i have a second concern, about why all the examples on the internet only use the antiforgery token in the edit and create scenarios (not in the deletion scenario), while i did not see any tutorial that uses the antiforgery token in the delete scenario ,, so is it necessary to do so (i think that we should do so !!!)Abshire
S
2

Modifying the answer by Bronx:

$.ajaxPrefilter(function (options, localOptions, jqXHR) {
    var token, tokenQuery;
    if (options.type.toLowerCase() !== 'get') {
        token = GetAntiForgeryToken();
        if (options.data.indexOf(token.name)===-1) {
            tokenQuery = token.name + '=' + token.value;
            options.data = options.data ? (options.data + '&' + tokenQuery) 
                : tokenQuery;
        }
    }
});

combined with this answer by Jon White

function GetAntiForgeryToken() {
  var tokenField = $("input[type='hidden'][name$='RequestVerificationToken']");
  if (tokenField.length == 0) { return null; 
  } else {
  return {
     name: tokenField[0].name,
     value: tokenField[0].value
  };
}

Edit sorry - realised I am re-inventing the wheel here SO asp-net-mvc-antiforgerytoken-over-ajax/16495855#16495855

Stevenstevena answered 11/6, 2013 at 5:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.