{"id":257,"date":"2011-02-28T19:19:09","date_gmt":"2011-02-28T19:19:09","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=257"},"modified":"2011-02-28T19:19:09","modified_gmt":"2011-02-28T19:19:09","slug":"impersonate_use","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2011\/02\/28\/impersonate_use\/","title":{"rendered":"Impersonate SharePoint User"},"content":{"rendered":"<p>This is a post that I&#8217;d imagine most people who have been developing with SharePoint will be aware of but I thought it might be worthwhile blogging about.<\/p>\n<p>I&#8217;ve seen code where people have tried to impersonate a user to perform actions against a SharePoint file by using the LogonUser function.<\/p>\n<pre lang=\"csharp\">\r\n[System.Runtime.InteropServices.DllImport(\"ADVAPI32.DLL\")]\r\npublic static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out int phToken);\r\n<\/pre>\n<p>When working with SharePoint, there is no need for this.  For starters, the above function requires you to know the user&#8217;s password.  Now, of course, there are good reasons why the business rules may require this and I&#8217;m not saying it shouldn&#8217;t be implemented that way.  For the scenarios where this is not required, there&#8217;s a very easy and simple way of impersonating a valid SharePoint user, without having to know their password.<br \/>\n<!--more--><br \/>\nOne simple way of impersonating another user is to wrap the <em>Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges<\/em> method around your code.  This will ensure all objects created within the method are executed as the application pool user.  There&#8217;s no point in wrapping this around code like the following sample.<\/p>\n<pre lang=\"csharp\">\r\nSPContext.Current.ListItem[\"a field\"] = \"a value\";\r\n<\/pre>\n<p>This will execute as the currently logged in user and not the elevated user.  To run as the elevated user it&#8217;s important to open the site or web objects while inside the method.<\/p>\n<pre lang=\"csharp\">\r\nSPSecurity.RunWithElevatedPrivileges(() =>\r\n{\r\n    using (SPSite site = new SPSite(SPContext.Current.Site.ID))\r\n    {\r\n        using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))\r\n        {\r\n            SPListItem item = web.GetListItem(SPContext.Current.ListItemServerRelativeUrl);\r\n            item[\"a field\"] = \"a value\";\r\n        }\r\n    }\r\n});\r\n<\/pre>\n<p>But I digress, the point of this post was to demonstrate how to impersonate a specific user and not the application pool user for the current web application.<\/p>\n<p>If the code above executed as the application pool user, the following will execute as the user Joe Bloggs.  There is no exception handling, so obviously if the user did not exist in the site the code would fail but it shows you how to impersonate an individual user by using their token.<\/p>\n<pre lang=\"csharp\">\r\nSPUser jbloggsUser = SPContext.Current.Web.AllUsers[\"domain\\jbloggs\"];\r\nusing (SPSite site = new SPSite(SPContext.Current.Site.ID, jbloggsUser.UserToken))\r\n{\r\n    using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))\r\n    {\r\n        SPListItem item = web.GetListItem(SPContext.Current.ListItemServerRelativeUrl);\r\n        item[\"a field\"] = \"a value\";\r\n    }\r\n}\r\n<\/pre>\n<p>The difference with this code is with the SPSite constructor, where we pass the SPUserToken object for Joe Bloggs.  The containing code will now execute as this user.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a post that I&#8217;d imagine most people who have been developing with SharePoint will be aware of but I thought it might be worthwhile blogging about. I&#8217;ve seen code where people have tried to impersonate a user to &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2011\/02\/28\/impersonate_use\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[3],"tags":[14,15,81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-49","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/257"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=257"}],"version-history":[{"count":4,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/257\/revisions"}],"predecessor-version":[{"id":262,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/257\/revisions\/262"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}