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
siteStr = "http://mysharepointsite/";

 
 

//we just need to get a handle to the site for us

//to get the system account user token

SPSite tempSite = new
SPSite(siteStr);

 
 

SPUserToken systoken = tempSite.SystemAccount.UserToken;

 
 

using
(SPSite site = new
SPSite(siteStr, systoken))

{

   using
(SPWeb web = site.OpenWeb())

   {

       //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
SPSite(siteStr, userToken);

 
 

SPWeb w = s.OpenWeb();

Console.WriteLine("Currently logged in as: "
+

                  w.CurrentUser.ToString() +

                  "("
+ w.CurrentUser.Name + ")"

                 );

Complete code follows:

private
static
void
impersonateTest()

{

   string
siteStr = "http://mysharepointsite/";

   SPSite tempSite = new
SPSite(siteStr);

   SPUserToken systoken = tempSite.SystemAccount.UserToken;

   using
(SPSite site = new
SPSite(siteStr, systoken))

   {

       using
(SPWeb web = site.OpenWeb())

       {

           //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
static
void
switchUser(SPWeb web, string
siteStr, string
user)

{

   //impersonate somebody else

   SPUserToken userToken = web.AllUsers[user].UserToken;

   SPSite s = new
SPSite(siteStr, userToken);

   SPWeb w = s.OpenWeb();

   Console.WriteLine("Currently logged in as: "
+

                     w.CurrentUser.ToString() +

                     "("
+ w.CurrentUser.Name + ")"

                    );

}

 

 

From: http://www.sharepointdeveloperhq.com/2009/04/how-to-programmatically-impersonate-users-in-sharepoint/

How to Programmatically Impersonate Users in SharePoint的更多相关文章

  1. 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 ...

  2. Creating a SharePoint Sequential Workflow

    https://msdn.microsoft.com/en-us/library/office/hh824675(v=office.14).aspx Creating a SharePoint Seq ...

  3. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q147-Q151)

    Question  147 Your company has an existing SharePoint 2010 public-facing Web site. The Web site runs ...

  4. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q144-Q146)

    Question  144 You are planning a feature upgrade for a SharePoint 2010 farm. The original feature wi ...

  5. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q112-Q115)

    Question  112 You are designing a public-facing SharePoint 2010 Web site for an elementary school th ...

  6. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q16-Q18)

    Question 16 You are designing a SharePoint 2010 solution to manage statements of work. You need to d ...

  7. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q59-Q62)

    Question 59You are designing a collection of Web Parts that will be packaged into a SharePoint 2010 ...

  8. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q75-Q77)

    Question 75You are designing a feature for a SharePoint 2010 solution that will be activated by defa ...

  9. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q78-Q80)

    Question 78 You are designing an application configuration approach for a custom SharePoint 2010 app ...

随机推荐

  1. 崽崽帮www.zaizaibang.com精选1

    南京郊外免费旅游景点推荐!不花钱又好玩~ 南艺帅哥手绘的南京,想说不爱你都难! [快乐 你懂的]—太原市育华幼儿园小一班 昆明周边游:那些近在咫尺的梨园 弘雅小学开展一年级新生入学准备期活动 大班的主 ...

  2. bzoj4750: 密码安全

    Description 有些人在社交网络中使用过许多的密码,我们通过将各种形式的信息转化为 01 信号,再转化为整数,可以将这个 人在一段时间内使用过的密码视为一个长度为 n 的非负整数序列 A_1, ...

  3. jQuery整体架构源码解析(转载)

    jQuery整体架构源码解析 最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性, ...

  4. SQLite中的时间日期函数(转)

    SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间date()...........................产生日期tim ...

  5. windows和linux实现文件共享

    linux和windows实现共享,需要安装samba服务器 安装步骤: 1.查看是否已经安装samba rpm -q samba 2.如果已经安装,如果你想再次安装,可以卸载 rpm -e samb ...

  6. FreeRTOS知识点

    特点: FreeRTOS任务不容许以任何方式从  任务中 实现函数中返回--绝对不能有return语句,也不能执行到函数末尾.如果任务不再需要,可以显示的删除. 一个任务函数可以用来创建若干个任何-- ...

  7. Tomcat Connector三种运行模式(BIO, NIO, APR)的比较和优化

    Tomcat Connector的三种不同的运行模式性能相差很大,有人测试过的结果如下: 这三种模式的不同之处如下: BIO: 一个线程处理一个请求.缺点:并发量高时,线程数较多,浪费资源. Tomc ...

  8. vbox下创建共享存储

    1.创建共享盘VBoxManage.exe createhd -filename D:\VM\linux01\ocr_vote.vdi -size 2048 -format VDI -variant ...

  9. Linux下搭建SVN服务器及自动更新项目文件到web目录(www)的方法

    首先搭建SVN服务器 1,安装SVN服务端 直接用apt-get或yum安装subversion即可(当然也可以自己去官方下载安装) sudo apt-get install subversion   ...

  10. 安装centOS分区的图解记录

    要将linux的boot loader安装在MBR上,才能起到多重引导的作用.