Asp.net mvc 4 MVC4 AntiForgery令牌cookie名称附加了随机字符串
我遇到了MVC4的问题Asp.net mvc 4 MVC4 AntiForgery令牌cookie名称附加了随机字符串,asp.net-mvc-4,antiforgerytoken,Asp.net Mvc 4,Antiforgerytoken,我遇到了MVC4的问题 @Html.AntiForgeryToken() html助手。在我的开发机器上,当我运行项目时,在检查头部(使用Fiddler)时,返回的令牌的名称是 __RequestVerificationToken 但当部署到IIS 7.5版(Windows 2008 R2)时,令牌名称如下所示: __RequestVerificationToken_L2V6b3JkZXI1 这是在哪里换的?是否因为我的应用程序未部署到IIS的“根文件夹”?例如,我的应用程序部署到 "ht
@Html.AntiForgeryToken()
html助手。在我的开发机器上,当我运行项目时,在检查头部(使用Fiddler)时,返回的令牌的名称是
__RequestVerificationToken
但当部署到IIS 7.5版(Windows 2008 R2)时,令牌名称如下所示:
__RequestVerificationToken_L2V6b3JkZXI1
这是在哪里换的?是否因为我的应用程序未部署到IIS的“根文件夹”?例如,我的应用程序部署到
"http://myserver/myapp" instead of "http://myserver"
我看了源代码后找到了答案:
http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.WebPages/Helpers/AntiForgeryConfig.cs
是的,因为我的应用程序被部署到一个路径,下面的代码附加了该路径的编码等价物。。。希望这个发现能帮你省去麻烦
// If the app path is provided, we're generating a cookie name rather than a field name, and the cookie names should
// be unique so that a development server cookie and an IIS cookie - both running on localhost - don't stomp on
// each other.
internal static string GetAntiForgeryCookieName(string appPath)
{
if (String.IsNullOrEmpty(appPath) || appPath == "/")
{
return AntiForgeryTokenFieldName;
}
else
{
return AntiForgeryTokenFieldName + "_" + HttpServerUtility.UrlTokenEncode(Encoding.UTF8.GetBytes(appPath));
}
}
使用AntiForgeryConfig类很容易解决此问题。请参阅下面的参考资料 您需要在Global.asax文件的Application_Start()事件下添加以下代码
if (AntiForgeryConfig.CookieName.Contains("__RequestVerificationToken"))
{
AntiForgeryConfig.CookieName = "__RequestVerificationToken";
}
我们能做些什么来解决这个问题。很抱歉,我正在向一个非常旧的线程添加注释。请指定您为解决此问题所执行的步骤
if (AntiForgeryConfig.CookieName.Contains("__RequestVerificationToken"))
{
AntiForgeryConfig.CookieName = "__RequestVerificationToken";
}