使用ValidateAntiForgeryToken与Ajax.ActionLinkValidateAntiForgeryToken、Ajax、ActionLink

2023-09-10 14:04:54 作者:八级心动

是否可以使用控制器上的动作ValidateAntiForgeryToken属性,当请求来自Ajax.ActionLink(使用HTTP POST方法?)。另一种方法似乎是手卷一个jQuery Ajax请求,但我很好奇,如果有是MVC的Ajax框架内的一种方式。

Is it possible to use the ValidateAntiForgeryToken attribute on a controller action when the request comes from Ajax.ActionLink (using Http Method Post?). The alternative appears to be to hand roll a JQuery Ajax request but I am curious if there is a way within the MVC Ajax framework.

推荐答案

我还没有看到一个烤的方式。你必须得到令牌成上升的POST数据。该防伪标记的ID(或名称,我不记得)是一样的每一次,但你必须是真实的谨慎,并确保防忘记你正在使用的是最近的一次呈现给客户端,令牌,该令牌可以在AJAX应用程序变得棘手。

I haven't seen a baked-in way. You have to get the token into the data that goes up in the POST. The antiforgery token's ID (or name, I can't remember) is the same each time, but you have to be real careful, and make sure that the anti forget token you are using is the most recent one rendered to the client, which can get tricky in an AJAX app.