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 ...
随机推荐
- OLDB读取excel的数据类型不匹配的解决方案(ZT)
1 引言 在应用程序的设计中,经常需要读取Excel数据或将Excel数据导入转换到其他数据载体中,例如将Excel数据通过应用程序导入SQL Sever等数据库中以备使用.笔者在开发“汽车产业链A ...
- Windows下一些奇怪安装问题的解决
你可能遇到过无法安装.Net Framework的问题,也许你也知道可以用微软的.Net Framework Cleanup Tool来解决,网上也流传着其他解决办法,然而有时候以上方法都不管用,此时 ...
- MyBatis入门学习教程-调用存储过程
一.提出需求 查询得到男性或女性的数量, 如果传入的是0就女性否则是男性 二.准备数据库表和存储过程 create table p_user( id int primary key auto_incr ...
- undefined reference to `_init'问题解决
今天利用CDT 的eclipse调试程序,遇到下面的问题: d:/plugin/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi ...
- mysql 5.7.11 yum 安装步骤
直接上步骤 首先你得有一台能够上外网的linux服务器 1:rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el6-5.noarch ...
- Python xlrd、xlwt、xlutils修改Excel文件
一.xlrd读取excel 这里介绍一个不错的包xlrs,可以工作在任何平台.这也就意味着你可以在Linux下读取Excel文件.首先,打开workbook: import xlrdwb = x ...
- Objective-C的 KVC和KVO
字面意思分别是: KVC是指key value coding,键值编码. KVO是指key value observing,键值观察. 直白的说法是: KVC就是将一个对象的属性及其值当做一个字典,可 ...
- [转载]VIM 教程:Learn Vim Progressively
文章来源:http://yannesposito.com/Scratch/en/blog/Learn-Vim-Progressively/ Learn Vim Progressively TL ...
- jquery 获取元素在浏览器中的绝对位置
代码详解 1,获取对象(自定义调整打开新窗口参照元素) var obj = $("#oButton"); 实例中我获取的对象是弹出窗口按钮,这样创建的新窗口就会根椐按钮的位置进行调 ...
- Android屏幕适配全攻略 (转载)
http://blog.csdn.net/jdsjlzx/article/details/45891551 https://github.com/hongyangAndroid/AndroidAuto ...