2

I have developed an application in c# using visual studio 2015, that copies some files from one directory (source) to another (destination) in general. My problem is that the source path is another computer in a domain. I wish to be able to access the directory and get my files, user the domain, username and password the source computer. I have seen some solution, but I can't get how they access the other computer. I used to get my files by using the Directory. GetDirectories (path), and I'm far too deep using it now and can't change it to smooth as. thanks for helping me with my problem I'm truly blocked for days now.

string[] folder1;
string[] folder2;
folder1 = Directory.GetDirectories(path);
foreach (string fld1 in folder1)
{
    folder2 = Directory.GetDirectories(fld);
    foreach(string fld2 in folder2)
    {
        for(int i = 0; i < MyList.Count(); i++)
        {
            if(fld2.Contains("nok") || fld2.Contains("Nok"))
                LNok = Directory.GetFiles(fld2, picList[i]);
            else
                Lok = Directory.GetFiles(fld2, picList[i]);
        }
    }
}
Mehdi.tou
  • 21
  • 1
  • 3
  • Please show us your code what you have tried so far and where the error occurs. – L. Guthardt Jul 13 '17 at 07:56
  • that's how i get my files, and it's linked to this every where in my application, it worked fine until i had to get files from a computer in a domain locked with a username and a password. – Mehdi.tou Jul 13 '17 at 08:18

1 Answers1

6

Since System.IO.File and System.IO.Directory methods don’t support passing credentials, the preferred solution is to impersonate an authorized user account. This requires to import two methods from the advapi32.dll and kernel32.dll using pInvoke:

//Impersonation functionality
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

//Disconnection after file operations
[DllImport("kernel32.dll")]
private static extern Boolean CloseHandle(IntPtr hObject);

The following code makes use of those methods to create a WindowsImpersonationContext

const int LOGON_TYPE_NEW_CREDENTIALS = 9;
const int LOGON32_PROVIDER_WINNT50 = 3;

//User token that represents the authorized user account
IntPtr token = IntPtr.Zero;

bool result = LogonUser("username", "domainname", "password",  LOGON_TYPE_NEW_CREDENTIALS , LOGON32_PROVIDER_WINNT50, ref token);

if (result == true)
{
    //Use token to setup a WindowsImpersonationContext 
    using (WindowsImpersonationContext ctx = new WindowsIdentity(token).Impersonate())
    {
        //Your file operations
        string[] files = Directory.GetFiles(@"\\remotemachine\share\folder");

        //Release the context, and close user token
        ctx.Undo();
        CloseHandle(token);
    }
}

Here you may find the MSDN documentation of the LogonUser function

Simone Cifani
  • 784
  • 4
  • 14
  • hello, i did try your proposition, there is no exception in run mode but i do not get any result, no files are loaded. Just like if the directory that i look in is empty, & i'm sure that it's not. – Mehdi.tou Jul 13 '17 at 11:32
  • I searched for similar post on stackoverflow and found [this post](https://stackoverflow.com/questions/5023607/how-to-use-logonuser-properly-to-impersonate-domain-user-from-workgroup-client). Take a look at the most voted answer, suggesting to use different parameters on LogonUser function. `const int LOGON_TYPE_NEW_CREDENTIALS = 9; const int LOGON32_PROVIDER_WINNT50 = 3; bool returnValue = LogonUser(user, domain, password, LOGON_TYPE_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, ref token);` – Simone Cifani Jul 13 '17 at 12:54
  • I'm already working with a backgroundworker, I'm afraid that if I implement the example you referred, I am going to get a conflict – Mehdi.tou Jul 13 '17 at 13:58
  • I was referring to the answer of takrl, there is no BackgroundWorker, just a suggestion on the LogonUser parameters. – Simone Cifani Jul 13 '17 at 14:25