how to include AntiForgeryToken in an ajax action link in mvc?

血红的双手。 提交于 2019-12-29 01:25:08

问题


I have the following code:

@Ajax.ActionLink("Delete", "Delete", 
new { id = item.ID, RequestVerificationToken=*What comes here?*}, 
new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "formsIndex" })

I want to add the verification token to the link without using javascript in client side, it seems like a redundant dependancy since i already own that value in server. Is there a proper way to do that?


回答1:


From the MSDN documentation (my emphasis)

HtmlHelper.AntiForgeryToken Method

Generates a hidden form field (anti-forgery token) that is validated when the form is submitted.

You need a form element to generate the anti-forgery token.

@Ajax.BeginForm("Delete", new { id = item.ID }, new AjaxOptions { UpdateTargetId = "formsIndex" }))
{
  @Html.AntiForgeryToken()
  <input type="submit" value="Delete" /> // style to look like a link if that's what you want
}


来源:https://stackoverflow.com/questions/29727420/how-to-include-antiforgerytoken-in-an-ajax-action-link-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!