Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 21st, 2012  |  syntax: None  |  size: 4.01 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Copy files over network via file share, user authentication
  2. [09:35:29]: [Step 1/3] Unhandled Exception: System.UnauthorizedAccessException: Access to the path '\192.168.0.76htdocspublic' is denied.
  3. [09:35:29]: [Step 1/3]    at DeployFileShare.Program.CopyDir(String source, String dest, String[] exclude, Boolean overwrite)
  4. [09:35:29]: [Step 1/3]    at DeployFileShare.Program.Deploy(String num, String source)
  5. [09:35:29]: [Step 1/3]    at DeployFileShare.Program.Main(String[] args)
  6. [09:35:29]: [Step 1/3] Process exited with code -532459699
  7.        
  8. AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
  9. WindowsIdentity idnt = new WindowsIdentity(username, password);
  10. WindowsImpersonationContext context = idnt.Impersonate();
  11.        
  12. AppDomain.CreateDomain("192.168.0.76").SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
  13. WindowsIdentity idnt = new WindowsIdentity("user", "pass");
  14. WindowsImpersonationContext context = idnt.Impersonate();
  15.        
  16. C:UsersAdministrator>DeployFileShare 1 R:BuildOutput_PublishedWebsitesWeb 2
  17. 1
  18. Deploy Started Web, version 21
  19. -- Deploy Prepared
  20. -- Deploying to 1
  21.  
  22. Unhandled Exception: System.Security.SecurityException: There are currently no l
  23. ogon servers available to service the logon request.
  24.  
  25.    at System.Security.Principal.WindowsIdentity.KerbS4ULogon(String upn)
  26.    at System.Security.Principal.WindowsIdentity..ctor(String sUserPrincipalName,
  27.  String type)
  28.    at DeployFileShare.Program.Authenticate(String server)
  29.    at DeployFileShare.Program.Deploy(String num, String source)
  30.    at DeployFileShare.Program.Main(String[] args)
  31. The Zone of the assembly that failed was:
  32. MyComputer
  33.        
  34. static void Main()
  35. {
  36. Copy();
  37. }
  38. static void Copy()
  39. {
  40. AppDomain.CreateDomain(GetServerInfo(server, "server")).SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
  41.             WindowsIdentity idnt = new WindowsIdentity(GetServerInfo(server, "user"), GetServerInfo(server, "pass"));
  42.             WindowsImpersonationContext context = idnt.Impersonate();
  43. string source = "C:\someDir";
  44. string dest = "\192.168.0.76shareFolder"
  45. string[] sourceFiles = Directory.GetFiles(source, "*", SearchOption.AllDirectories);
  46.             foreach (string file in sourceFiles)
  47.             {
  48.                 string local = file.Replace(source, "");
  49.                 if (exclude.Contains(local))
  50.                     continue;
  51.                 if (!Directory.Exists(Path.GetDirectoryName(dest + "\" + local)))
  52.                     Directory.CreateDirectory(Path.GetDirectoryName(dest + "\" + local));
  53.                 File.Copy(file, dest + "\" + local, overwrite);
  54.                 Console.WriteLine("-- -- [copied] {0} -> {1}", file, dest + "\" + local);
  55.             }
  56. }
  57.        
  58. Private Sub Open_Remote_Connection(ByVal strComputer As String, ByVal strUserName As String, ByVal strPassword As String)
  59.     Dim ProcessStartInfo As New System.Diagnostics.ProcessStartInfo
  60.     ProcessStartInfo.FileName = "net"
  61.     ProcessStartInfo.Arguments = "use \" & strComputer & "c$ /USER:" & strUsername & " " & strPassword
  62.     ProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden
  63.     System.Diagnostics.Process.Start(ProcessStartInfo)
  64.     System.Threading.Thread.Sleep(2000)
  65. End Sub
  66.        
  67. /// <summary>
  68. /// Exécute une fonction en empruntant les credentials
  69. /// </summary>
  70. private T ApplyCredentials<T>(Func<T> func)
  71. {
  72.     IntPtr token;
  73.  
  74.     if (!LogonUser(
  75.         _credentials.UserName,
  76.         _credentials.Domain,
  77.         _credentials.Password,
  78.         LOGON32_LOGON_INTERACTIVE,
  79.         LOGON32_PROVIDER_DEFAULT,
  80.         out token))
  81.     {
  82.         Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
  83.     }
  84.  
  85.     try
  86.     {
  87.         // On doit être impersonifié seulement le temps d'ouvrir le handle.
  88.         using (var identity = new WindowsIdentity(token))
  89.         using (var context = identity.Impersonate())
  90.         {
  91.             return func();
  92.         }
  93.     }
  94.     finally
  95.     {
  96.         CloseHandle(token);
  97.     }
  98. }
  99.  
  100. // ...
  101.  
  102. if (_credentials != null)
  103. {
  104.     return this.ApplyCredentials(() => File.Open(path, mode, access, share));
  105. }
  106.  
  107. return File.Open(path, mode, access, share);