How to revert multiple impersonations? When there are nested impersonations (as in exec as user = 'user1'; exec as user = 'user2'; exec as user = 'user1';), is there a way to revert all of them without having to use the revert command multiple times (e.g. revert all;)?

Or at least, if one must use revert repeatedly, is there a way to check if there is an active impersonation so one could loop the command as many times as needed (e.g. while @@IMPERSONATION_COUNT &gt; 0 revert;)?
You could assign the current context to a cookie then revert to that cookie when you desire to.