Search code examples
sql-serverimpersonation

SQL Server : is it possible to find out the original user who issued setuser 'impersonate_username'?


SQL Server allows a login with sufficient rights to issue a setuser command and impersonate another user. For e.g I can connect as user1 and if I have syadmin role, I can issue a setuser 'user2'. Now my context gets switched to user2.

But is there any way to figure out that, using T-SQL:

  1. that there is 'impersonation' going on?
  2. Who the original user was i.e in this case user1?

Solution

  • I think you want the function ORIGINAL_LOGIN(). Also, I think you'll find sys.sysprocesses.loginame remains constant no matter what happens with setuser or execute as.