简体   繁体   中英

impersonate multiple users at the same time

I'm making a service in .net that copy files from one domain (Domain A) to other domain (Domain B) both domain need credentials in order to connect to them so i'm using impersonation.

using (new Impersonator(usr_name_source, domain_source, password_source))

the impersonation is working only for one domain each time, so actually i cannot work how to impersonate both domains at the same time in order to copy the files so i'm trying this:

using (new Impersonator(usr_name_source, domain_source, password_source))//server authentication
                                        {
                                            using (new Impersonator(usr_name_target, domain_target, password_target))//server authentication
                                            {
                                                DeleteOldFiles(targetPath);
                                                Copy(sourcePath, targetPath);
                                            }
                                        }

but its not working as when i impersonate the inside new Impersonator(usr_name_target, domain_target, password_target)

it forget the outer impersonation.

does anyone has any idea how to do that without mapping drives etc...?

You can only impersonate one user at a time, so your current solution will not work. Basically you are trying to connect to two different network resources. You can P/Invoke WNetAddConnection2 function to connect to network resource of different domain and perform operation as needed. See here for details on WNetAddConnection2: https://msdn.microsoft.com/en-us/library/windows/desktop/aa385413(v=vs.85).aspx

Go through this post to see how you P/Invoke WNetAddConnection2: How to provide user name and password when connecting to a network share

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM