SharePoint v3:忘掉模拟用户Impersonate,SPSecurity.RunWithElevatedPrivileges来了

回顾:

在SharePoint V2 大家应该都用过模拟用户Impersonate这个功能,

这个功能用来暂时提升某个用户的权限,比如某个普通用户的本来不能修改某个列表的值,但是我们功能需要在修改。

缺点:

    我们使用这个模拟用户功能时候,经常是明文保存用户名密码,是个安全隐患。

    更加气愤的是,据我所知,在匿名用户访问状态下面,根本不能够模拟成功。

V3解决办法:

Elevation of Privilege 

Elevation of privilege is a new feature of that enables you to programmatically perform actions in code using an increased level of privilege. The Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges method enables you to supply a delegate that runs a subset of code in the context of an account with higher privileges than the current user.

A standard usage of RunWithElevatedPrivileges is:

SPSecurity.RunWithElevatedPrivileges(delegate()

{

    // do things assuming the permission of the "system account"

});

Frequently, to do anything useful within SharePoint you'll need to get a new SPSite object within this code to effect the changes.  For example:

SPSecurity.RunWithElevatedPrivileges(delegate()

{

    using (SPSite site = new SPSite(web.Site.ID))

    {

       // do things assuming the permission of the "system account"

    }

});

Although elevation of privilege provides a powerful new technique for managing security, it should be used with care. You should not expose direct, uncontrolled mechanisms for people with low privileges to circumvent the permissions granted to them. 

 

注意:

SPSite要在代码块里面创建,而不能使用当前的SPSite

// Uses the App poll creds with the SPUser's identity reference of user

SPSecurity.RunWithElevatedPrivileges(delegate()

{

// Gets a new security context using

using (SPSite site = new SPSite( SPContext.Current.Site.ID ))

{

using (SPWeb thisWeb = site.OpenWeb())

{

thisWeb.AllowUnsafeUpdates = true;

SPItem item = //web.GetListItem(this.Page.Request.Url.ToString());

thisWeb.GetList(ListName).GetItemById(ID);

item[FieldName] = (item[FieldName] == null) ? 1 : (double)item[FieldName] + 1;

item.Update();

 

writer.Write("Visited Counter. Current:(" + item[FieldName].ToString() + ")");

}

}

});

    运行那一段代码的用户是应用程序池的用户,(在IIS里面设置,避免了明文保存)

    

    注意要关闭SPSite /SPWeb ,可以参考: http://msdn2.microsoft.com/en-us/library/aa973248.aspx

结束:

经过测试,匿名用户也能成功。我的浏览计数功能就使用了该段代码。

 

MSDN参考:

Elevation of Privilege : http://msdn2.microsoft.com/en-us/library/aa543467.aspx

Best Practices: Using Disposable Windows SharePoint Services Objects

转自:http://www.cnblogs.com/cleo/archive/2007/04/06/sharepoint_v3_impersonate_spsecurity_runwithelevatedprivileges.html

转载于:https://www.cnblogs.com/llbofchina/archive/2007/04/17/717065.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/536342.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

螺旋方阵问题【数组】

输入n&#xff0c;输出n阶螺旋方阵&#xff0c;下面为5阶螺旋方阵&#xff1a;1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 下面为我的代码&#xff1a; #include <cstdio> #include <iostream> #include &…

马鞍点问题【数组】

如果在一矩阵中元素A[i][j]满足A[i][j]为第i行的最小值&#xff0c;第j行的最大值&#xff0c;则称这个元素为这个矩阵的马鞍点&#xff0c;求m*n矩阵所有的马鞍点。若需求一个矩阵的所有马鞍点&#xff0c;其实只需将矩阵的每行的最小值与每列的最大值分别求出存在相应的数组中…

求二叉树节点个数、叶子节点、节点层次与宽度

需实现&#xff1a;&#xff08;1&#xff09;输出二叉树b的节点个数 &#xff08;2&#xff09;输出二叉树b的叶子节点个数 &#xff08;3&#xff09;求二叉树b中指定节点值&#xff08;假设所有节点值不同&#xff09;的节点的层次。 &#xff08;4&#xff09;利用层次遍历…

【数学】Chaarshanbegaan at Cafebazaar

题目描述 Chaarshanbegaan is a gathering event at Cafebazaar similar to TGIF events at Google. Some entertainment programs like pantomime, foosball, Xbox/PS4, and several board games are part of the event. You are going to set up a dart game in Chaarshanbe…

【思维】Congestion Charging Zone

题目描述 Tehran municipality has set up a new charging method for the Congestion Charging Zone (CCZ) which controls the passage of vehicles in Tehran’s high-congestion areas in the congestion period (CP) from 6:30 to 19:00. There are plate detection came…

【二分】LED

题目描述 A Light-Emitting Diode (LED) is a semiconductor light source, which emits light when an electric current of voltage higher than a threshhold is applied to its leads. ACM R&D recently reported that they have succesfully developed a new LED, na…

【模拟】Thanks, TuSimple!

题目链接&#xff1a;http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId5979 Thanks, TuSimple! Time Limit: 1 Second Memory Limit: 65536 KB In the very first sentence of the very first problem, we would like to give our sincere thanks to TuSimple,…

【二维差分】Monitor

Monitor 题目&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid6514 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 163840/163840 K (Java/Others) Total Submission(s): 600 Accepted Submission(s): 190 Problem Description Xiaoteng has a la…

【二分+二维前缀和】Largest Allowed Area

Largest Allowed Area 时间限制: 1 Sec 内存限制: 128 MB 提交: 146 解决: 54 [提交] [状态] [命题人:admin] 题目描述 A company is looking for land to build its headquarters. It has a lot of money and can buy as many land patches as it needs. Its goal, howev…

【数学】Floating-Point Hazard

Floating-Point Hazard 时间限制: 1 Sec 内存限制: 128 MB 提交: 106 解决: 42 [提交] [状态] [命题人:admin] 题目描述 Given the value of low, high you will have to find the value of the following expression: If you try to find the value of the above express…

【线段树】Segment Tree

Segment Tree 时间限制: 1 Sec 内存限制: 512 MB 提交: 107 解决: 23 [提交] [状态] [命题人:admin] 题目描述 Mcginn opens the code which he wrote 25 years ago. Clever Mcginn wants to know how many positive interger n satisfied that the maximum c can reach w…

扶桑号战列舰【RMQ+分治】

扶桑号战列舰 时间限制: 1 Sec 内存限制: 128 MB Special Judge 提交: 197 解决: 63 [提交] [状态] [命题人:admin] 题目描述 众所周知&#xff0c;一战过后&#xff0c;在世界列强建造超无畏级战列舰的竞争之中&#xff0c;旧日本海军根据“个舰优越主义”&#xff0c;建造了扶…

大凤号装甲空母【找规律+矩阵快速幂】

大凤号装甲空母 时间限制: 1 Sec 内存限制: 128 MB 提交: 108 解决: 15 [提交] [状态] [命题人:admin] 题目描述 大凤号航空母舰很喜欢算术。 它&#xff0c;是旧日本海军中最为先进的航空母舰。 它&#xff0c;是旧日本海军中最为短命的航空母舰。 同时&#xff0c;她还是最平…

Degree Sequence of Graph G【模拟】

Degree Sequence of Graph G 时间限制: 1 Sec 内存限制: 128 MB 提交: 362 解决: 92 [提交] [状态] [命题人:admin] 题目描述 Wang Haiyang is a strong and optimistic Chinese youngster. Although born and brought up in the northern inland city Harbin, he has deep lov…

【动态规划】魔法石矿

【动态规划】魔法石矿 时间限制: 1 Sec 内存限制: 64 MB 提交: 116 解决: 27 [提交] [状态] [命题人:admin] 题目描述 为了找到回家的路&#xff0c;张琪曼施展魔法&#xff0c;从高维空间召唤出了一种叫作“读者”的生物&#xff0c;据说“读者”这种生物无所不能&#xff0c;…

简单类及成员实例【C#】

简单类及成员实例&#xff08;C#&#xff09; 题目描述 简单类及成员实例。定义了如下图所示类Student&#xff0c;根据下图和给出代码&#xff0c;补写缺失的代码。 using System; namespace sample{ class Student { public string studentid;//学号 p…

c#随机数的产生与输出【C#】

c#随机数的产生与输出 题目描述 编写一个实例方法Method01。该方法使用Random类随机产生n个3位数字&#xff08;如636&#xff09;的随机正整数&#xff0c;并把产生的随机数存入数组中并输出该数组int num Convert.ToInt32(Console.ReadLine()); using System; using System…

C# teacher类【C#】

C# teacher类 题目描述 定义一个教师类Teacher&#xff0c;具体要求如下&#xff1a; 1、私有字段工号no&#xff08;string&#xff09;、姓名name&#xff08;string&#xff09;、出生日期birthday&#xff08;DateTime&#xff09;、性别sex&#xff08;SexFlag&#xff0…

接口实例(C#,IShape)【C#】

接口实例&#xff08;C#,IShape&#xff09; 题目描述 接口实例。接口和类如下图所示&#xff0c;根据给出代码&#xff0c;补写缺失的代码&#xff0c;然后在Program类的静态Main方法中验证所实现的类。 using System; namespace Myinterface { public interface IShape…

1439: 2.4.5 Fractions to Decimals 分数化小数

1439: 2.4.5 Fractions to Decimals 分数化小数 时间限制: 1 Sec 内存限制: 64 MB提交: 194 解决: 13题目描述 写一个程序&#xff0c;输入一个形如N/D的分数(N是分子&#xff0c;D是分母)&#xff0c;输出它的小数形式。 如果小数有循环节的话&#xff0c;把循环节放在一对圆…