我在.Net 4和IIS6(Win2003服務器)上運行MVC3 Web應用程序。間歇性,我們可以捕捉到以下錯誤消息:MVC3 AntiForgeryToken相關問題,深入框架
System.ArgumentException: Value cannot be null or an empty string.
Parameter name: serializedToken
at System.Web.Helpers.AntiForgeryDataSerializer.Deserialize(String serializedToken)
at System.Web.Helpers.AntiForgeryWorker.GetAntiForgeryTokenAndSetCookie(HttpContextBase httpContext, String salt, String domain, String path)
at System.Web.Helpers.AntiForgeryWorker.GetHtml(HttpContextBase httpContext, String salt, String domain, String path)
at System.Web.Helpers.AntiForgery.GetHtml(HttpContextBase httpContext, String salt, String domain, String path)
at System.Web.Mvc.HtmlHelper.AntiForgeryToken(String salt, String domain, String path)
at System.Web.Mvc.HtmlHelper.AntiForgeryToken()
at ASP._Page_Views_Introduction_GetStarted_cshtml.Execute() in d:\MyProject\Views\ControllerName\ViewName.cshtml:line 43
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.StartPage.RunPage()
at System.Web.WebPages.StartPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
在視圖中我們有一些代碼
using (Html.BeginForm("ActionName", "ControllerName"))
{
@Html.AntiForgeryToken()
... other codes ...
}
和接收操作方法看起來像
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ActionName()
{
// other logic here
return RedirectToAction("Something", "Something");
}
我覺得編碼應該是正確的。任何人都曾經歷過類似的事情?
在我的代碼中,我不會觸及任何cookie數據。 – hardywang 2012-08-30 00:29:31
但你有沒有檢查使用Firebug發送的cookies? __ RequestVerificationTokenxxxx__ cookie是什麼樣的?它可能已被相同域中的另一個應用程序所改變,因爲Cookie在默認情況下並未指定路徑,而是在域上的不同應用程序之間共享。 – Jacob 2012-08-30 19:22:34