title: Copy files over network via file share, user authentication
body: <p>I am building a .net C# console program to deploy file to a windows file share server (folder that is being shared). The path is :: <code>\\192.168.0.76\htdocs\public</code> </p>
  
  <p>On running I am getting the error:</p>
  
  <pre><code>[09:35:29]: [Step 1/3] Unhandled Exception: System.UnauthorizedAccessException: Access to the path '\\192.168.0.76\htdocs\public' is denied.
  [09:35:29]: [Step 1/3]    at DeployFileShare.Program.CopyDir(String source, String dest, String[] exclude, Boolean overwrite)
  [09:35:29]: [Step 1/3]    at DeployFileShare.Program.Deploy(String num, String source)
  [09:35:29]: [Step 1/3]    at DeployFileShare.Program.Main(String[] args)
  [09:35:29]: [Step 1/3] Process exited with code -532459699
  </code></pre>
  
  <p>I think I need to authenticate myself. I've come across this:</p>
  
  <pre><code>AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
  WindowsIdentity idnt = new WindowsIdentity(username, password);
  WindowsImpersonationContext context = idnt.Impersonate();
  </code></pre>
  
  <p>I've also tried:</p>
  
  <pre><code>AppDomain.CreateDomain("192.168.0.76").SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
  WindowsIdentity idnt = new WindowsIdentity("user", "pass");
  WindowsImpersonationContext context = idnt.Impersonate();
  </code></pre>
  
  <p>I am not sure how to use it. When I run the application I get:</p>
  
  <pre><code>C:\Users\Administrator&gt;DeployFileShare 1 R:\BuildOutput\_PublishedWebsites\Web 2
  1
  Deploy Started Web, version 21
  -- Deploy Prepared
  -- Deploying to 1
  
  Unhandled Exception: System.Security.SecurityException: There are currently no l
  ogon servers available to service the logon request.
  
     at System.Security.Principal.WindowsIdentity.KerbS4ULogon(String upn)
     at System.Security.Principal.WindowsIdentity..ctor(String sUserPrincipalName,
   String type)
     at DeployFileShare.Program.Authenticate(String server)
     at DeployFileShare.Program.Deploy(String num, String source)
     at DeployFileShare.Program.Main(String[] args)
  The Zone of the assembly that failed was:
  MyComputer
  </code></pre>
  
  <p>Here is the basic code:</p>
  
  <pre><code>static void Main()
  {
  Copy();
  }
  static void Copy()
  {
  AppDomain.CreateDomain(GetServerInfo(server, "server")).SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
              WindowsIdentity idnt = new WindowsIdentity(GetServerInfo(server, "user"), GetServerInfo(server, "pass"));
              WindowsImpersonationContext context = idnt.Impersonate();
  string source = "C:\\someDir";
  string dest = "\\192.168.0.76\shareFolder"
  string[] sourceFiles = Directory.GetFiles(source, "*", SearchOption.AllDirectories);
              foreach (string file in sourceFiles)
              {
                  string local = file.Replace(source, "");
                  if (exclude.Contains(local))
                      continue;
                  if (!Directory.Exists(Path.GetDirectoryName(dest + "\\" + local)))
                      Directory.CreateDirectory(Path.GetDirectoryName(dest + "\\" + local));
                  File.Copy(file, dest + "\\" + local, overwrite);
                  Console.WriteLine("-- -- [copied] {0} -&gt; {1}", file, dest + "\\" + local);
              }
  }
  </code></pre>
  
  <p>The code copy system in the for loop works, I've tested it on my local system.</p>
  
  <p>If anyone knows how I should use WindowsIdentity and WindowsIdentity to get this to work please enlighten me. I've been looking around and window's documentation doesn't help much.</p>
  
  <p>Basically how can I copy to a remote directory being shared by logging into the system?</p>