I am currently developing an MVC application in ASP.net. I am using AJAX.ActionLink to provide a delete link in a list of records, however this is very insecure. I have put this:
<AcceptVerbs(HttpVerbs.Post)>
Over the function to do the deleting, which stops the function being called simply by a URL. However, the other security hole that still exists is that if i were to make a basic html page with this content:
<form action="http://foo.com/user/delete/260" method="post">
<input type="submit" />
</form>
It would still be perfoming a post, but from a different location.
Is it possible to use the AntiForgeryToken with an AJAX ActionLink? If so, is this a secure approach? Are there more security holes i haven't realised?
You can use AntiForgeryToken with Ajax.ActionLink but you need to manually insert the AntiForgeryToken into the header of your request like so:
Then, we can use $.ajaxPrefilter to insert it into the header:
I wrote a post about it here. Hope this helps!
Have a look at this blog post.
Update: The link has been fixed.
To piggyback on the
$.ajaxPrefilter
answers, I added the token to bothoptions
andoriginalOptions
rather than thejqXHR
headers. This does require the token to be somewhere in a form on your page.Keep in mind that this will add this token to every single AJAX request on your page, so you may want to filter by the
options.url
string oroptions.type == 'POST'
.Use AntiForgeryToken with Ajax.ActionLink
In addition to jjwhite01 response; to insert the token in Form data, use
option.data
in PrefilterI don't know about the AJAX ActionLink specifically, but it is possible from a WebForms page to post to an MVC action with the
[AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken]
attributes.You can use reflection to get at the MVC methods used to set the cookie and matching form input used for the MVC validation.
See this answer: Using an MVC HtmlHelper from a WebForm
I haven't used any ajax helpers myself, but I don't see any reason why you cannot use a link. Personally I would use an onload event handler to unobtrusively create a link from the form itself, and then remove the form.