asp.net-mvc C 如何设置AntiForgeryToken cookie路径
HtmlHelper.AntiForgeryToken方法.
[ObsoleteAttribute("This method is deprecated. Use the AntiForgeryToken() method instead. To specify
|
不推荐使用允许覆盖字符串路径的前 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. 解决方法 查看弃用消息:“This method is deprecated. Use the AntiForgeryToken() method instead. To specify a custom domain for the generated cookie,use the configuration element. To specify custom data to be embedded within the token,use the static AntiForgeryConfig.AdditionalDataProvider property.” 它告诉我们,只要回读伪造令牌,我们就可以验证其他参数.因此,即使我们无法在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 – 为什么Global.asax事件在我的ASP.NET网站没有触
- asp.net – 如何从复选框列表中获取最新的选定值?
- asp.net – 如何从日历控件中获取所选日期?
- asp.net-mvc C 已经使用相同的参数类型定义了一个名为“Cre
- asp.net-mvc C ASP.NET MVC WebSite中的ERR_EMPTY_RESPONSE
- asp.net – Web Forms MVP项目有哪些好的资源?
- 如何在ASP.NET 5中添加一个TypeScript绝对类型的定义?
- asp.net-mvc – LabelFor和TextBoxFor不生成相同的id
- 如何在ASP.NET MVC中配置3个级别的URL?
- asp.net C MVC4 C ContextDependentView C 这是什么意思?
- asp.net-mvc C 在我的ASP.NET MVC网站中缓存不能
- asp.net-mvc – MVC应用程序调试错误:viewstate
- asp.net-mvc – DDD原理和ASP.NET MVC项目设计
- 过滤ASP.NET Core API中的属性
- asp.net – HttpWebRequest正在为404抛出异常
- asp.net-web-api C Web API / MVC 6中的安全JSON
- asp.net-mvc – MVC应用程序中的随机数生成
- asp.net-mvc-3 – 剃刀引擎 – 如何根据不同的条
- MVC .Net Cascade在使用EF Code First Approach时
- 使用ASP.NET Identity 2.0和MVC 5进行自定义单点
- asp.net-core C 如何使用ASP.NET注册OData
- asp.net C MVC4 C ContextDependentView C
- asp.net-mvc-3 C 如何从ASP.NET MVC#输出中
- asp.net-mvc C 已经使用相同的参数类型定义
- asp.net-mvc C 使用AD的ASP.NET MVC表单Aut
- ASP.Net C AJAX UpdatePanel中的Javascript
- asp.net-mvc C ASP.NET MVC中的WebApi [Fro
- asp.net C 适用于多个用户的EWS通知中心
- asp.net-mvc C 未在ELMAH中记录的错误
- asp.net-mvc C MVC应用程序中的随机数生成
