使用MVC3 AntiforgeryToken的链接?

编程入门 行业动态 更新时间:2024-10-19 10:23:32
本文介绍了使用MVC3 AntiforgeryToken的链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的MVC3的网站,我有一个包含多个链接的页面。这些链接的所有链接回我的网站上的路线,用不同的ID值和结构是这样:

On my MVC3 site, I have a page that contains several links. These links all link back to a route on my site, with different ID values and are structured as such:

例如: www.mysite/links/33351/3

我想利用MVC3的antiforgerytoken机制,这样我可以保证从所有请求 www.mysite/links/33351/3 链接索引页。

I'd like to take advantage of the MVC3's antiforgerytoken mechanism so that I can ensure that all requests to www.mysite/links/33351/3 from the link index page.

我熟悉如何将令牌添加到窗体,但这些都是独立的链接。

I'm familiar with how to add the token to a form, however these are all stand-alone links.

我怎样才能做到这一点?

How can I accomplish this?

推荐答案

由于采用了上述评论这帮助我解决这个问题。

Thanks to the comments above which helped me solve this.

从本质上讲,我创建了一个JavaScript函数来处理项的点击次数。我的网页上每个环节都有一个ID,所以我只是通过对JS函数提交表单传递的ID:

Essentially, I created a Javascript function to process the item clicks. Each link on my page has an ID, so I simply passed the ID through to the JS function which submits the form:

<script type="text/javascript"> <!-- function doClick(itemID) { document.getElementById('hid_ItemID').value = itemID; // add whatever additional js type processing needed here - ie. analytics, etc. document.forms[0].submit(); } //--> </script>

窗体本身包含了MVC防伪标记标记:

The form itself contains the MVC anti-forgery token tag:

@using (Html.BeginForm("DoRequest", "DoItemClickRq", FormMethod.Post, new { target = "_blank" })) { @Html.AntiForgeryToken() <input type="hidden" id="hid_ItemID" name="hid_ItemID" value="" /> . . .

控制器方法:

[ValidateAntiForgeryToken] [HttpPost] public ActionResult DoItemRequest() { int itemListID = 0; int pagePositionNumber = 0; int.TryParse(Request["hid_ItemID"], out itemListID);

。。

. . .

更多推荐

使用MVC3 AntiforgeryToken的链接?

本文发布于:2023-11-24 20:59:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626793.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链接   AntiforgeryToken

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!