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. Jeesite的cahche工具类

    本CacheUtils主要是基于shiro的cache进行处理. 其他选择: 类似的我们可以选择java cache ,spring cahche等方案.                   再进一步 ...

  2. Hadoop总结篇之五---模块间是怎么驱动执行的

    在MRv1中,各个模块间驱动运行的方式是函数调用的方式.这是同步的过程,上一模块调用下一模块函数后,等待其执行.效率不高. 在MRv2中做了改进,yarn基于事件驱动的并发模型.在详细介绍前,先看下图 ...

  3. Oracle 存储过程 split 代码实现

    实现 字符串分割, 算法 如下: 算法 1: DECLARE remove_column myvarray_list; x ); sub ); i NUMBER; j NUMBER; c NUMBER ...

  4. 解析JDK 7的动态类型语言支持

    http://www.infoq.com/cn/articles/jdk-dynamically-typed-language

  5. fuser 命令小结

    fuser 概述 fuser命令是用来显示所有正在使用着指定的file, file system 或者 sockets的进程信息. 例一: #fuser –m –u /mnt/usb1 /mnt/us ...

  6. CentOS7:安装Puppet

    环境说明: 192.168.154.137 master.localdomain #Puppet Server 192.168.154.138 agent1.localdomain #Puppet A ...

  7. android中Json的一些应用

    JSON(JavaScript Object Notation) :一种轻量级的数据交换格式,基于JavaScript的一个子集. JSON采用完全独立于语言的文本格式,使JSON成为理想的数据交换语 ...

  8. C# 报表设计器 (winform 设计端)开发与实现生成网页的HTML报表

    记得2010年之前,公司的项目基本上都要用到报表,以前我们常用的方法就是针对客户的需求来定制化开发(基本上是死写代码)来实现,经常导致项目经常性的延期,因为客户的需求经常会变化,随着用户的使用认知度的 ...

  9. 【摘】 pt-query-digest工具一解

    原文 http://blog.csdn.net/seteor/article/details/24017913 1.percona-toolkit安装 wget http://www.percona. ...

  10. Linux学习笔记(二)

    1.tzselect无法是使用 vim /usr/bin/tzselect 将 ${TZDIR=pwd}改为${TZDIR=/usr/share/zoneinfo} 2.sudo apt-get in ...