Reputation: 71
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...?
Upvotes: 1
Views: 1642
Reputation: 1100
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
Upvotes: 1