获取Html.AntiForgeryToken引发错误“发送HTTP标头后服务器无法修改cookie". [英] Getting Html.AntiForgeryToken throws error "Server cannot modify cookies after HTTP headers have been sent"

查看:80
本文介绍了获取Html.AntiForgeryToken引发错误“发送HTTP标头后服务器无法修改cookie".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行以下代码时:

When I attempt to run the following code:

<form id="__SendAjaxAntiForgery" action="#" method="post"><%= Html.AntiForgeryToken()%></form>

出现以下错误
发送HTTP标头后,服务器无法修改cookie."

堆栈跟踪读取

at System.Web.HttpResponse.BeforeCookieCollectionChange()
at System.Web.HttpCookieCollection.Set(HttpCookie cookie)
at System.Web.Helpers.AntiXsrf.AntiForgeryWorker.GetFormInputElement(HttpContextBase httpContext)
at System.Web.Helpers.AntiForgery.GetHtml()
at System.Web.Mvc.HtmlHelper.AntiForgeryToken()
at ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer)

仅当我打开2个选项卡并且注销其中的一个选项卡,然后重新登录选项卡1时,才会发生这种情况(这样做时我会得到一个新的AntiForgeryToken),然后转到选项卡2并刷新.

This only happens when I have 2 tabs open and on one of the tabs I logout and then log back in on tab 1(and in doing so I get a new AntiForgeryToken) and then go to tab 2 and refresh.

当我尝试获取"Html.AntiForgeryToken();"时发生异常.

The exception is occuring when I attempt to get "Html.AntiForgeryToken();"

推荐答案

因此,这与防伪令牌无关.Anti-Forgery令牌在此实例中正常工作.我们有一个无法正确创建的工具栏,该工具栏导致异常,进而将其重定向到注销.一旦修复了问题,确保正确创建了工具栏,那么Anti-Forgery令牌便可以正常工作.当我开始此过程时,我唯一不知道的是方法上注释的顺序是MVC处理规则的顺序.因此,我们有自己的规则,因此我将这些规则移到了最前面,这导致我以不同的角度来思考问题.

So this has nothing to do with the Anti-Forgery token. The Anti-Forgery token was working correctly in this instance. We had a toolbar that wasn't getting created correctly and that was causing an exception which in turn was redirecting them to the logout. Once we fixed making sure that the toolbar was created correctly then the Anti-Forgery token worked correctly. The only thing that I wasn't aware of when I started this process was that the order the annotations on the method is the order that MVC processes the rules. So we had our own rules and I moved those up the order and that led me to thinking about the problem in a different light.

这篇关于获取Html.AntiForgeryToken引发错误“发送HTTP标头后服务器无法修改cookie".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆