{"id":94625,"date":"2016-11-02T07:00:00","date_gmt":"2016-11-02T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=94625"},"modified":"2019-03-13T10:33:15","modified_gmt":"2019-03-13T17:33:15","slug":"20161102-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20161102-00\/?p=94625","title":{"rendered":"What happens if you call RevertToSelf when not impersonating?"},"content":{"rendered":"<p>A customer wanted to know what happens if you call <code>Revert&shy;To&shy;Self<\/code> from a thread that is not impersonating. &#8220;Does the call succeed or fail? This particular scenario is not explicitly discussed in the documentation. We have a bunch of places in our code that say <code>if (impersonating) RevertToSelf();<\/code> and we were wondering whether the <code>if<\/code> test was really necessary.&#8221; <\/p>\n<p>The answer to the question is that calling <code>Revert&shy;To&shy;Self<\/code> when the thread is not impersonating will return success without doing anything (because the thread is already not impersonating). <\/p>\n<p>However, that doesn&#8217;t mean that you can blindly remove all your <code>if<\/code> tests. You don&#8217;t want to over-revert either. Consider: <\/p>\n<pre>\n\/\/ Error checking elided for expository purposes.\nvoid DoSomething()\n{\n bool impersonating = false;\n\n if (!ThreadIsAlreadyImpersonating() &amp;&amp;\n     ImpersonationIsNeeded()) {\n   StartImpersonating();\n   impersonating = true;\n }\n\n DoWork();\n\n if (impersonating) {\n  RevertToSelf();\n }\n}\n<\/pre>\n<p>If you remove the <code>if (impersonating)<\/code> and unconditionally revert, then you have a security defect if the thread was already impersonating, because your modified code will unconditionally revert and prematurely end the existing impersonation.  <\/p>\n<p>So yes, it&#8217;s okay to call <code>Revert&shy;To&shy;Self<\/code> when the thread is not impersonating, but that doesn&#8217;t relieve you of the responsiblity of knowing when to revert. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nothing, but be careful.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-94625","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Nothing, but be careful.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=94625"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94625\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=94625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=94625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=94625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}