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 ...
随机推荐
- Maven间接依赖冲突解决办法
如果项目中maven依赖太多,由于还有jar之间的间接依赖,所以可能会存在依赖冲突.依赖冲突大部分都是由于版本冲突引起的,查看maven的依赖关系,可以找到引起冲突的间接依赖 如上图,通过Depend ...
- MySQL服务 - MySQL程序的配置文件、参数、变量查看
查看配置文件及读取顺序 MySQL的配置文件以.cnf结尾,可能会有多个,而不同版本的MySQL程序的读取配置文件的路径也都不同,要想获取MySQL读取配置文件的顺序可以通过以下指令查看: shell ...
- 传入url地址请求服务器api,浏览器显示图片
@RequestMapping("/proxyImage") public void proxyImage(HttpServletRequest request, HttpServ ...
- windows WSABUF 结构在IOCP 中应用时两个成员变量的意义
WSABUF 结构的原型如下: typedef struct __WSABUF { u_long len; char FAR *buf; } WSABUF, *LPWSABUF; 该结构在IOCP 中 ...
- css相对定位+浮动实现元素位置互换
1.设置元素透明度 opacity:0.5; // w3c filter:alpha(opacity=50); //IE 2 position:relative; float:left; 一起使用的效 ...
- python中的thread
转载自: http://blog.sina.com.cn/s/blog_9f488855010198vn.html 正确与否未验证 python中得thread的一些机制和C/C++不同:在C/C++ ...
- (String). Word Pattern
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- 查询oracle连接数 对应的 应用程序
select b.MACHINE, b.PROGRAM , count(*) from v$process a, v$session b where a.ADDR = b.PADDR and b ...
- Java Docs
1 Java Docs on Oracle: 1.1 Online(EN): JavaSE6 http://docs.oracle.com/javase/6/docs/api/index.htm ...
- C#利用WIN32实现按键注册
using System.Runtime.InteropServices; using System.Collections.Generic; using System; using System.W ...