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. OLDB读取excel的数据类型不匹配的解决方案(ZT)

    1 引言  在应用程序的设计中,经常需要读取Excel数据或将Excel数据导入转换到其他数据载体中,例如将Excel数据通过应用程序导入SQL Sever等数据库中以备使用.笔者在开发“汽车产业链A ...

  2. Windows下一些奇怪安装问题的解决

    你可能遇到过无法安装.Net Framework的问题,也许你也知道可以用微软的.Net Framework Cleanup Tool来解决,网上也流传着其他解决办法,然而有时候以上方法都不管用,此时 ...

  3. MyBatis入门学习教程-调用存储过程

    一.提出需求 查询得到男性或女性的数量, 如果传入的是0就女性否则是男性 二.准备数据库表和存储过程 create table p_user( id int primary key auto_incr ...

  4. undefined reference to `_init'问题解决

    今天利用CDT 的eclipse调试程序,遇到下面的问题: d:/plugin/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi ...

  5. mysql 5.7.11 yum 安装步骤

    直接上步骤 首先你得有一台能够上外网的linux服务器 1:rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el6-5.noarch ...

  6. Python xlrd、xlwt、xlutils修改Excel文件

    一.xlrd读取excel 这里介绍一个不错的包xlrs,可以工作在任何平台.这也就意味着你可以在Linux下读取Excel文件.首先,打开workbook:    import xlrdwb = x ...

  7. Objective-C的 KVC和KVO

    字面意思分别是: KVC是指key value coding,键值编码. KVO是指key value observing,键值观察. 直白的说法是: KVC就是将一个对象的属性及其值当做一个字典,可 ...

  8. [转载]VIM 教程:Learn Vim Progressively

    文章来源:http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/   Learn Vim Progressively   TL ...

  9. jquery 获取元素在浏览器中的绝对位置

    代码详解 1,获取对象(自定义调整打开新窗口参照元素) var obj = $("#oButton"); 实例中我获取的对象是弹出窗口按钮,这样创建的新窗口就会根椐按钮的位置进行调 ...

  10. Android屏幕适配全攻略 (转载)

    http://blog.csdn.net/jdsjlzx/article/details/45891551 https://github.com/hongyangAndroid/AndroidAuto ...