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. CentOS 6主机上的RStudio Server安装步骤

    1. 安装EPEL库 yum -y install epel-release 2. 安装R yum install R 3. 安装OpenSSL yum install openssl098e 4. ...

  2. Vim常用命令总结

    Vim常用命令总结: 下文是小编自己整理的关于日常linux vim编程过程中用的一些常用命令,资料参考鸟哥的 如果有需要的小伙伴可以参考一下!(底部有下载地址) 一. 一般模式可用的按钮说明 1.1 ...

  3. php中的gethostbyname函数有问题

    在根据域名获取ip的批量执行中,gethostbyname有些域名得到的ip是不正确的,不知道是不是版本的bug. 解决办法是,使用执行命令的方式获取 echo exec("host dom ...

  4. Java单例模式实现(线程安全)

    package com.javaee.corejava; /** * 线程安全的单例模式 * @author miaoyf * */ public class Singleton { /** * 私有 ...

  5. opengl入门学习

    OpenGL入门学习 说起编程作图,大概还有很多人想起TC的#include <graphics.h>吧? 但是各位是否想过,那些画面绚丽的PC游戏是如何编写出来的?就靠TC那可怜的640 ...

  6. HTTP返回码总结(转)

    HTTP协议状态码表示的意思主要分为五类 ,大体是 :   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~  1×× 保留   2×× 表示请求成功地接收   3×× 为完成请求客户需进一步 ...

  7. android 代码生成selector drawable

    public static StateListDrawable makeSelector(Context context, int idNormal, int idPressed, int idFoc ...

  8. Silverlight 缓存控制策略

    通常我们用html或asp.net页面承载Silverlight包,由于浏览器缓存机制的存在,使得你的应用更新时,客户见到往往是旧版的程序,而完全禁止缓存,每次打开页面都要重新下载xap,对于体积较大 ...

  9. 介绍Unreal Engine 4中的接口(Interface)使用C++和蓝图

    这个教程是从UE4 Wiki上整理而来. 在C++中直接使用Interface大家应该很熟悉.只是简单先定义一个个有虚函数的基类,然后在子类中实现相应的虚函数.像这样的虚函数的基类一般概念上叫接口.那 ...

  10. IOS 中openGL使用教程1(openGL ES 入门篇 | 搭建openGL环境)

    OpenGL版本 iOS系统默认支持OpenGl ES1.0.ES2.0以及ES3.0 3个版本,三者之间并不是简单的版本升级,设计理念甚至完全不同,在开发OpenGL项目前,需要根据业务需求选择合适 ...