FileSystemWatcher running under impersonated user

a 夏天 提交于 2019-12-07 14:34:01

问题


I have an c# winform application that runs under a local account but needs to monitor folders on a domain. I am using slightly modified code from here to copy the files and that works fine. Can similar code be used with the FileSystemWatcher set up impersonation so I can monitor a folder on a domain?


回答1:


Yes, there is a good impersonation class here, include this class in your project and than simply place your FileSystemWatcher within a using block like this:

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

   <code that executes under the new context>

   ...
}



回答2:


You could:

Apply permissions to the domain folders that allow non-domain users access (i.e. Everyone.)

Logon with a domain account that has permissions and run the winform app.

Refactor the FileSystemWatcher code into a windows service and run it under a domian account with sufficient permissions.

Impersonate a domain account within the existing code, there are several good solutions on code project.



来源:https://stackoverflow.com/questions/2562542/filesystemwatcher-running-under-impersonated-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!