如何AllowUnsafeUpdates
来源:互联网 发布:经济下行 知乎 编辑:程序博客网 时间:2024/05/01 22:05
AllowUnsafeUpdates的定义
根据msdn的解释,当在SharePoint 页面HttpContext上下文中通过GET请求更新SharePoint contentDB时,为了允许更新数据库或者避免安全验证,"AllowUnsafeUpdates" 需要设置成true,
否则,会抛The security validation for this page is invalid异常.
该属性默认为false, 微软引入这个属性是为防止受到cross-site scripting 攻击. 但是,某些情况下, 可能需要更新一些不安全的数据, 这时候可以把它置成false
什么时候使用
在SharePoint 页面HttpContext上下文中通过GET请求更新SharePoint contentDB时
什么时候不需要使用
在一个Console Application, Class Library... ,在这些工程中,HTTPContext.Current = null, AllowUnsafeUpdates默认都为true, 所以不需要设置这个属性.
AllowUnsafeUpdates注意事项:
. 在HTTPContext 上下文中使用, 且操作是Get请求(即页面不跳转-Post) , POST 请求不需要这个属性,因为 FormDigest控件的存在, 使用完后置回成false(安全考虑)
. 但有打破继承的操后后,该属性会重置成默认值-false, 所以后续如果有更新操作需要重新置成true.
Bad piece of code:SPList sharedPictures = curWeb.Lists["Shared Pictures"];sharedPictures.Title = "My Pictures";curWeb.AllowUnsafeUpdates = true; //--> no help!sharedPictures.BreakRoleInheritance(true);ReducePermissonsOnLibrary(sharedPictures); //---> crash!
Working piece of code:SPList sharedPictures = curWeb.Lists["Shared Pictures"];sharedPictures.Title = "My Pictures";sharedPictures.BreakRoleInheritance(true);CurWeb.AllowUnsafeUpdates = true; //BreakRoleInheritance set AllowUnsafeUpdates back to false!ReducePermissonsOnLibrary(sharedPictures);
. 该属性置成true后,如果后续操作抛异常, 在catch里面该属性也会重置成默认值 -false, 需要注意.
SPWeb web = ...;try{ web.AllowUnsafeUpdates = true; throw new Exception("Something went really wrong underway !");}catch (Exception ex){ // At this point the web.AllowUnsafeUpdates is false. Before we can update the item we will need to set it to true ! web.AllowUnsafeUpdates = true; SPListItem errorItem = web.Lists["ErrorLog"].Items.Add(); errorItem["Title"] = "Web.Title update failed"; errorItem["Description"] = ex.ToString(); errorItem.Update();}
. 当一个页面继承WebPartPage类后,不需要再使用AllowUnsafeUpdates.
- 如何AllowUnsafeUpdates
- SharePoint的AllowUnsafeUpdates介绍
- 关于SPList的Update及AllowUnsafeUpdates
- 使用 Web Service 上传文件至 SPS Document Library 的问题及解决: SPWeb.AllowUnsafeUpdates = true
- 如何?
- 如何
- 如何。。。
- 如何推广,如何SEO
- 如何做人,如何处世。
- 如何做?如何作?
- 微信小程序如何如何
- 如何学习
- 如何找工作
- 如何提高效率
- 如何提高工作效率?
- 如何回去?
- 如何平安夜
- 如何调试
- PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)
- 李莉娜非诚勿扰三拒男嘉宾 网友表痛惜
- 图片加载框架--ImageLoader实现(二)
- Debian Linux字符界面 Boost 库下载 使用 [大三四八九月实习]
- 【解题报告】 HDU 4390 Number Sequence -- 容斥原理(不好理解)
- 如何AllowUnsafeUpdates
- C语言指针5分钟教程
- 【黑马程序员】多线程的方法重入问题
- windows 上命令行帐户管理
- Android中Looper/Handler学习笔记
- 算法中对于用七次比较完成5个元素的排序
- FineUI学习笔记1
- 踮起脚尖爱
- a href=#与 a href=javascript:void(0) 的区别