Advertisement
Guest User

Untitled

a guest
Mar 14th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. using System;
  2. using System.Configuration;
  3. using System.Diagnostics;
  4. using System.Text.RegularExpressions;
  5. using System.Security;
  6. using System.Net;
  7. using System.Security.Principal;
  8. using System.Reflection;
  9. using System.IO;
  10.  
  11.  
  12. public static void SetAlternateUser(string userName, string password, string domain)
  13. {
  14. // thread safe singleton code
  15. lock (threadSafeLock)
  16. {
  17. if (_ie == null)
  18. {
  19. StartIE();
  20. }
  21.  
  22. _ie.Close();
  23. _ie = null;
  24.  
  25.  
  26. // fill the NetworkCredeitials object that we use for impersonation
  27. if (string.IsNullOrEmpty(domain))
  28. {
  29. alternateUserCredentials = new NetworkCredential(userName, password);
  30. }
  31. else
  32. {
  33. alternateUserCredentials = new NetworkCredential(userName, password, domain);
  34. }
  35.  
  36. // Prepare to launch
  37. ProcessStartInfo psi = new ProcessStartInfo();
  38. psi.UserName = userName;
  39. psi.Password = SecurePassword(password);
  40. if (!string.IsNullOrEmpty(domain))
  41. {
  42. psi.Domain = domain;
  43. }
  44. psi.UseShellExecute = false;
  45. psi.LoadUserProfile = true;
  46. psi.FileName = "c:\Program Files\Internet Explorer\iexplore.exe";
  47. psi.Arguments = "about:blank";
  48.  
  49.  
  50. // launch
  51. Process proc = new Process();
  52. proc.StartInfo = psi;
  53. proc.Start();
  54.  
  55. // Time to become an imposter
  56. hToken = IntPtr.Zero;
  57. hTokenDuplicate = IntPtr.Zero;
  58.  
  59. if (Win32.LogonUser(alternateUserCredentials.UserName, alternateUserCredentials.Domain, alternateUserCredentials.Password, 2 /*LOGON32_LOGON_INTERACTIVE*/, 0 /*LOGON32_PROVIDER_DEFAULT*/, out hToken))
  60. {
  61. if (Win32.DuplicateToken(hToken, 2, out hTokenDuplicate))
  62. {
  63. windowsIdentity = new WindowsIdentity(hTokenDuplicate);
  64. impersonationContext = windowsIdentity.Impersonate();
  65. System.Threading.Thread.Sleep(1000);
  66.  
  67. _ie = IE.AttachToIE(Find.ByUrl("about:blank"));
  68.  
  69. }
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement