{"id":1311,"date":"2012-12-13T17:19:40","date_gmt":"2012-12-13T17:19:40","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1311"},"modified":"2012-12-13T17:19:40","modified_gmt":"2012-12-13T17:19:40","slug":"impersonating-current-app-pool-user","status":"publish","type":"post","link":"http:\/\/www.stuartroberts.net\/index.php\/2012\/12\/13\/impersonating-current-app-pool-user\/","title":{"rendered":"Impersonating Current App Pool User"},"content":{"rendered":"<p>Using SharePoint it&#8217;s easy to run a block of code with elevated permissions:<\/p>\n<pre lang=\"csharp\">\r\nSPSecurity.RunWithElevatedPrivileges(()=>\r\n{\r\n    using (SPSite elevatedSite = new SPSite(siteId))\r\n    {\r\n        using (SPWeb elevatedWeb = elevatedSite.OpenWeb(wedId))\r\n        {\r\n            \/\/ operations using elevated SPWeb object...\r\n        }\r\n    }\r\n});\r\n<\/pre>\n<p>This is fine for SharePoint related impersonations.  Any code executed within the elevated block that authenticates using an object instantiated outside the <em>RunWithElevatedPrivileges<\/em> block will run using the context of the current user and not the elevated account.  So for example, if you wanted to make a web method call using a single user (or connect to a database) and not the current context, the following code block will achieve this for you:<br \/>\n<!--more--><\/p>\n<pre lang=\"csharp\">\r\nusing (WindowsImpersonationContext context = WindowsIdentity.Impersonate(System.IntPtr.Zero))\r\n{\r\n    try\r\n    {\r\n        \/\/ code to run under the context of the current application pool identity.\r\n\r\n    }\r\n    catch (Exception ex)\r\n    {\r\n        \/\/ error loging, etc.\r\n        throw;\r\n    }\r\n    finally\r\n    {\r\n        context.Undo();\r\n    }\r\n}\r\n<\/pre>\n<p>Of course, within SharePoint, if you want a set of processes to run under the context of a specific user, it would be better to contain the code within a service application (for example) which will run under the context of the defined managed account and not the current user context.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using SharePoint it&#8217;s easy to run a block of code with elevated permissions: SPSecurity.RunWithElevatedPrivileges(()=> { using (SPSite elevatedSite = new SPSite(siteId)) { using (SPWeb elevatedWeb = elevatedSite.OpenWeb(wedId)) { \/\/ operations using elevated SPWeb object&#8230; } } }); This is fine &hellip; <a href=\"http:\/\/www.stuartroberts.net\/index.php\/2012\/12\/13\/impersonating-current-app-pool-user\/\">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":[1],"tags":[14],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-l9","_links":{"self":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1311"}],"collection":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1311"}],"version-history":[{"count":3,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1311\/revisions"}],"predecessor-version":[{"id":1314,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1311\/revisions\/1314"}],"wp:attachment":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1311"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}