How to Programmatically Impersonate Users in SharePoint
Sometimes when creating SharePoint web or console applications, you may need to execute specific code blocks in another user's context.
Impersonating users in SharePoint will require a couple of things:
- the account that the web or console app uses has privileges to impersonate other users (typically this would be the system account)
- specific users' user tokens
Step 1: Log in as the system account, or get a handle to the system account in your code
string //we just need to get a handle to the site for us //to get the system account user token SPSite tempSite = new SPUserToken systoken = tempSite.SystemAccount.UserToken; using { using { //right now, logged in as Site System Account Console.WriteLine("Currently logged in as: " web.CurrentUser.ToString()); //add your code here } } |
Step 2: Before you impersonate, get the user token of the user you are switching to. For example:
//get this current user's user token SPUserToken userToken = web.AllUsers[user].UserToken; //create an SPSite object in the context of this user SPSite s = new SPWeb w = s.OpenWeb(); Console.WriteLine("Currently logged in as: " w.CurrentUser.ToString() + "(" ); |
Complete code follows:
private { string SPSite tempSite = new SPUserToken systoken = tempSite.SystemAccount.UserToken; using { using { //right now, logged in as Site System Account Console.WriteLine("Currently logged in as: " web.CurrentUser.ToString()); switchUser(web, siteStr, "BlackNinjaSoftware/MatthewCarriere"); switchUser(web, siteStr, "BlackNinjaSoftware/ShereenQumsieh"); switchUser(web, siteStr, "BlackNinjaSoftware/DonabelSantos"); } } } private { //impersonate somebody else SPUserToken userToken = web.AllUsers[user].UserToken; SPSite s = new SPWeb w = s.OpenWeb(); Console.WriteLine("Currently logged in as: " w.CurrentUser.ToString() + "(" ); } |
How to Programmatically Impersonate Users in SharePoint的更多相关文章
- Programmatically Disable Event Firing on List Item Update in SharePoint 2010
1. Microsoft.SharePoint.dll Create EventFiring.cs 1.Right-click on the project, select Add and click ...
- Creating a SharePoint Sequential Workflow
https://msdn.microsoft.com/en-us/library/office/hh824675(v=office.14).aspx Creating a SharePoint Seq ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q147-Q151)
Question 147 Your company has an existing SharePoint 2010 public-facing Web site. The Web site runs ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q144-Q146)
Question 144 You are planning a feature upgrade for a SharePoint 2010 farm. The original feature wi ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q112-Q115)
Question 112 You are designing a public-facing SharePoint 2010 Web site for an elementary school th ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q16-Q18)
Question 16 You are designing a SharePoint 2010 solution to manage statements of work. You need to d ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q59-Q62)
Question 59You are designing a collection of Web Parts that will be packaged into a SharePoint 2010 ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q75-Q77)
Question 75You are designing a feature for a SharePoint 2010 solution that will be activated by defa ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q78-Q80)
Question 78 You are designing an application configuration approach for a custom SharePoint 2010 app ...
随机推荐
- [Linux] - centos使用mount + nfs 远程共享存储
服务端安装nfs 1.使用yum安装nfs yum install nfs-utils nfs-utils-lib -y 如果安装过程出现这样的错误: 得先安装lvm2 yum install -y ...
- 多表数据连接 Left join
一个我写的实例:其中多表连接,一共连接了3个表.使用聚集函数SUM,用到了GROUP BY SELECT a.[UserID],b.[Name],sum (c.[Money]+c.[Bank])as ...
- JAVA并发编程
拜读了一篇很详尽的博文,特此转载http://www.cnblogs.com/dolphin0520/p/3920373.html, 并做了一些更正. 一.内存模型的相关概念. 大家都知道,计算机在执 ...
- 【转】STM32中的抢占优先级、响应优先级概念
STM32(Cortex-M3)中有两个优先级的概念--抢占式优先级和响应优先级,有人把响应优先级称作'亚优先级'或'副优先级',每个中断源都需要被指定这两种优先级. 具有高抢占式优先级的中断可以在具 ...
- 1.ARM的基础知识
ARM简述 ARM公司既不生产芯片也不销售芯片,它只出售芯片技术授权.ARM技术具有很高的性能和功效,因而容易被厂商接受.同时,合作伙伴的增多,可获得更多的第三方工具.制造和软件支持,这又会使整个系统 ...
- linux mint 崩溃
换完linux mint 今天突然崩溃了.出现如下错误 因为是双屏.一个屏幕显示一般,这么不重要.搜了一下,找到解决方案 解决办法 ctrl+atl+f1 login sudo apt-get ins ...
- [POJ2761] Feed the dogs (Treap)
题目链接:http://poj.org/problem?id=2761 题目大意:给你n个数,m次查询,m次查询分别是a,b,k,查询下表从a到b的第k小元素是哪个.这m个区间不会互相包含. Trea ...
- Ejabberd作为推送服务的优化手段
AVOS Cloud目前还在用Ejabberd做Android的消息推送服务.当时选择Ejabberd,是因为Ejabberd是一个发展很长时间的XMPP实现,并且基于Erlang,设想能在我们自主研 ...
- 单片网络接口芯片W5100的原理与应用
随着计算机网络技术的发展,作为全球最大计算机网络——I n t e r ac t已成为当今信息社会重要的基础信息设施.在工业测控.智能仪器.智能家庭等领域,大量应用嵌入式设备接人 I n t e r ...
- SQL Server 数据库备份
declare @filename varchar(1024) declare @SQLDB varchar(50) declare @path varchar(1024) set @path = N ...