使用MVC3 AntiforgeryToken的链接? [英] Using the MVC3 AntiforgeryToken with links?
问题描述
在我的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.com/links/33351/3
我想利用MVC3的antiforgerytoken机制,这样我可以保证从所有请求 www.mysite.com/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.com/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的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!