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 ...
随机推荐
- mysql密码重置
忘记了Mysql的root用户密码,尝试连接时提示: mysqladmin: connect to server at 'localhost' failed ERROR : Access denied ...
- [JBoss] - 解决URI提交时乱码问题
JBoss 7 AS解决url提交数据乱码的问题: 打开jboss-as-7.1.1.Final\standalone\configuration\standalone.xml文件,在<exte ...
- JS跨域解决iframe高度自适应(IE8/Firefox/Chrome适用)
参考园友的js跨越实现,有提到三种方式: 1. 中间页代理方式,利用iframe的location.hash 参见:http://www.5icool.org/a/201203/a1129.html ...
- 【python】遍历类的所有成员
# -*- coding: utf-8 -*- #sharejs.com提供代码,转载请注明出处 class Site(object): def __init__(self): self.title ...
- asp.net动态设置标题title 关键字keywords 描述descrtptions
推荐的简单做法如下: protected void Page_Load(object sender, EventArgs e){//Page titlePage.Title = "This ...
- Python入门5
装饰器 装饰器英文Decorator,自身是一个函数,用来包装其它的函数,实现在不改变原来代码的情况下,增加新的功能,返回一个修改后的函数对象, 装饰器功能: 1.装饰器也是函数 2.在不改变原有代码 ...
- Displaying Bitmaps Efficiently 显示图片相关
http://developer.android.com/training/displaying-bitmaps/index.html .手机内存资源有限 .Bitmap占用的内存大 .App有时需要 ...
- 5. Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- Unity代码设置shader属性
主要是用到了Material.SetTexture这个方法,具体可以参考如下: 举个例子,比如我想用代码动态控制Skybox/Cubemap下的Cubemap,可以这样写: public Cubema ...
- makefile 函数集
1 if 函数 语法 $(if CONDITION,THEN-PART[,ELSE-PART]) 功能 第一个参数"CONDITION",在函数执行时忽略其前导和结尾空字符,如果包 ...