How to Impersonate a user in managed code?

前端 未结 5 1951
逝去的感伤
逝去的感伤 2020-12-09 00:29

Given a username and password how do i impersonate that user and run some code as that user.

And by managed i mean without pinvokes or dllimports

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 01:31

    All of the examples I've seen fail to take into account the fact that the logon type is not a one size fits all solution.

    For example this will only work if the user you are impersonating has permission to logon to the target system. Not always the case when accessing a remote SQL Server box. LOGON32_LOGON_INTERACTIVE

    NetworkClearText is the only one that works consistently for use with SQL Server connections. - No clear text does not mean it is passing credentials in an insecure manner.

    When on a workgroup and you need to impersonate a domain user NewCredentials is the one that works. (not tested with SQL Server connections)

提交回复
热议问题