开发者

Logoff after logonuser on C#

开发者 https://www.devze.com 2023-04-10 20:11 出处:网络
I use advapi32.dll\'s logonusermethod to access data over our network. I know it change the thread\'s user to the information i give i开发者_如何转开发t, but i was wondering if there\'s a way to rever

I use advapi32.dll's logonuser method to access data over our network.

I know it change the thread's user to the information i give i开发者_如何转开发t, but i was wondering if there's a way to reverse it.

I want to access the data and then return to the local user credentials.


Some time ago I created a small impersonator class.

Basically you wrap your code to execute under another user simply inside a using block:

using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) )
{
   ...

   <code that executes under the new context>

   ...
}

Worked very well for my projects.


You can call RevertToSelf.

That said, there is something to be said for spinning up a dedicated thread for the impersonation task, and terminating it when the impersonation work is complete. This will segregate the impersonation work so that if any callbacks or messages are processed on the main thread they will be performed in the context of the principal user rather than the impersonated user. In fact, the more I think about this the stronger I feel that a dedicated thread is the solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号