c# - Windows Impersonation: A Flaw in the Ointment -
in journey master nuances of user impersonation in windows first had issue getting impersonation remote database occur @ (see this question) figured out. next hurdle undoing/cancelling/reverting (choose favorite verb) impersonation.
i have tried couple different impersonation libraries seem credible me:
- phil harding's impersonator
- matt johnson's simpleimpersonation
the results identical both libraries. best practices dictate using logon32_logon_new_credentials logon type (see windows api logonuser function) remote db connection. when here sample code produces:
// scenario begin impersonation. local user = mydomain\myuser db reports: mydomain\impersonateduser end impersonation. local user = mydomain\myuser db reports: mydomain\impersonateduser << not expected here!!
the workaround have found use logon32_logon_interactive logon type , this:
// scenario b begin impersonation. local user = mydomain\impersonateduser << expected, not wanted! db reports: mydomain\impersonateduser end impersonation. local user = mydomain\myuser db reports: mydomain\myuser
from terse description of windowsimpersonationcontext.undo method sure seems should have worked in scenario a.
is possible revert using logon32_logon_new_credentials logon type?
thanks input harry johnston (in comments attached question) , phil harding (in separate communication) able determine sql server connection pooling culprit here. since pooling determined uniqueness of connection string, varying connection string (e.g. reversing order of parameters within, or adding space on end) observed behaviors expected.
===== test same conn string: true begin impersonation local user: mydomain\msorens db reports: mydomain\testuser end impersonation local user: mydomain\msorens db reports: mydomain\testuser <<<<< still impersonating !! ===== test same conn string: false begin impersonation local user: mydomain\msorens db reports: mydomain\testuser end impersonation local user: mydomain\msorens db reports: mydomain\msorens <<<<< wanted
Comments
Post a Comment