asp.net-mvc – 如何设置AntiForgeryToken cookie路径
不推荐使用允许覆盖字符串路径的前 HtmlHelper.AntiForgeryToken方法. [ObsoleteAttribute("This method is deprecated. Use the AntiForgeryToken() method instead. To specify a custom domain for the generated cookie,use the <httpCookies> configuration element. To specify custom data to be embedded within the token,use the static AntiForgeryConfig.AdditionalDataProvider property.",true)] public MvcHtmlString AntiForgeryToken( string salt,string domain,string path ) 告诉您使用< httpCookies>.但是httpCookies Element没有PATH的设置. 这是对这种方法的弃用的疏忽吗?覆盖此cookie路径的最佳方法是什么? (手动?)在虚拟应用程序中运行网站不会隐式地将应用程序路径添加到__RequestVeririfcation cookie. 解决方法查看弃用消息:
它告诉我们,只要回读伪造令牌,我们就可以验证其他参数.因此,即使我们无法在cookie中设置路径,我们也可以将路径设置为令牌内的属性.稍后要验证它,例如: public class AdditionalDataProvider : IAntiForgeryAdditionalDataProvider { public string GetAdditionalData(HttpContextBase context) { return AdditionalData(context); } public bool ValidateAdditionalData(HttpContextBase context,string additionalData) { var currentData = AdditionalData(context); return currentData == additionalData; } private static string AdditionalData(HttpContextBase context) { var path = context.Request.ApplicationPath; return path; } } 当asp.net生成令牌时,它将存储该应用程序的当前路径(或您要验证的任何其他唯一值) 另外,查看AntiforgeryConfig.cs的代码,如果应用程序在虚拟目录中运行,它将默认在cookie的名称中添加该虚拟目录: private static string GetAntiForgeryCookieName() { return GetAntiForgeryCookieName(HttpRuntime.AppDomainAppVirtualPath); } // 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)); } } 所以它会是这样的: 意思是App2虽然可以从App1接收令牌,但它将无法读取它们,因为它只会查找App2验证令牌. HTH (编辑:鄂州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.Net WebAPI与Ajax进行跨域数据交互时Cookies数据的传递
- asp.net错误表单不能嵌套在元素表单中?
- asp.net-mvc-4 – 在asp.net mvc 4模型中更改验证触发顺序
- asp.net-web-api – MaxExpansionDepth,带有最新的webapi和
- asp.net – 干净的方式来防止输入按钮提交表单
- 编辑并在ASP.NET Web项目中继续
- asp.net-mvc-4 – ASP.NET MVC 4通过ActionLink传递对象变量
- ASP.NET OnClientClick =“return false;”不起作用
- ASP.NET成员资格 – 让用户使用以前的密码
- asp.net – 未知的服务器标签’ajaxToolkit:CalendarExten
- asp.net-mvc-2 – ASP.NET MVC2 ModelMetadataPr
- asp.net-mvc – 使用IIS7.5将网站中的URL绑定到另
- 有什么好的参考或工具可用于将ASP转换为ASP.NET?
- asp.net字符串分割函数使用方法分享
- asp.net – @ Url.Action在控制器中创建空值的参
- 点击图片,AJAX删除后台图片文件的实现代码(asp.n
- 在ASP.NET中下载文件的实现代码
- asp.net-mvc – 有什么技巧/技巧使用亚音速与Asp
- asp.net-mvc-3 – 用于ASP的Telerik扩展. NET MV
- asp.net-mvc-3 – 如何从ASP.NET MVC#输出中删除