Guest User

Untitled

a guest
Sep 8th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. How to Process Start with Impersonated Domain User
  2. public class ImpersonationHelper : IDisposable
  3. {
  4. IntPtr m_tokenHandle = new IntPtr(0);
  5. WindowsImpersonationContext m_impersonatedUser;
  6.  
  7. #region Win32 API Declarations
  8.  
  9. const int LOGON32_PROVIDER_DEFAULT = 0;
  10. const int LOGON32_LOGON_INTERACTIVE = 2; //This parameter causes LogonUser to create a primary token.
  11.  
  12. [DllImport("advapi32.dll", SetLastError = true)]
  13. public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
  14. int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
  15.  
  16. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  17. public extern static bool CloseHandle(IntPtr handle);
  18.  
  19. [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  20. public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
  21. int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
  22.  
  23. #endregion
  24.  
  25. /// <summary>
  26. /// Constructor. Impersonates the requested user. Impersonation lasts until
  27. /// the instance is disposed.
  28. /// </summary>
  29. public ImpersonationHelper(string domain, string user, string password)
  30. {
  31. // Call LogonUser to obtain a handle to an access token.
  32. bool returnValue = LogonUser(user, domain, password,
  33. LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
  34. ref m_tokenHandle);
  35. if (false == returnValue)
  36. {
  37. int ret = Marshal.GetLastWin32Error();
  38. throw new System.ComponentModel.Win32Exception(ret);
  39. }
  40.  
  41. // Impersonate
  42. m_impersonatedUser = new WindowsIdentity(m_tokenHandle).Impersonate();
  43. }
  44.  
  45. #region IDisposable Pattern
  46.  
  47. /// <summary>
  48. /// Revert to original user and cleanup.
  49. /// </summary>
  50. protected virtual void Dispose(bool disposing)
  51. {
  52. if (disposing)
  53. {
  54. // Revert to original user identity
  55. if (m_impersonatedUser != null)
  56. m_impersonatedUser.Undo();
  57. }
  58.  
  59. // Free the tokens.
  60. if (m_tokenHandle != IntPtr.Zero)
  61. CloseHandle(m_tokenHandle);
  62. }
  63.  
  64. /// <summary>
  65. /// Explicit dispose.
  66. /// </summary>
  67. public void Dispose()
  68. {
  69. Dispose(true);
  70. GC.SuppressFinalize(this);
  71. }
  72.  
  73. /// <summary>
  74. /// Destructor
  75. /// </summary>
  76. ~ImpersonationHelper()
  77. {
  78. Dispose(false);
  79. }
  80.  
  81. #endregion
  82. }
  83.  
  84. using (new ImpersonationHelper("xxx.blabla.com", "xxxx", "xxxx"))
  85. {
  86.  
  87. if (!string.IsNullOrEmpty(txtFilename.Text))
  88. Process.Start(txtFilename.Text);
  89.  
  90. }
  91.  
  92. System.Diagnostics.Process proc = new System.Diagnostics.Process();
  93. System.Security.SecureString ssPwd = new System.Security.SecureString();
  94.  
  95. proc.StartInfo.UseShellExecute = false;
  96. proc.StartInfo.FileName = "filename";
  97. proc.StartInfo.Arguments = "args...";
  98. proc.StartInfo.Domain = "domainname";
  99. proc.StartInfo.UserName = "username";
  100.  
  101. string password = "test";
  102.  
  103. for (int x = 0; x < password.Length; x++)
  104. {
  105. ssPwd.AppendChar(password[x]);
  106. }
  107.  
  108. proc.StartInfo.Password = ssPwd;
  109. proc.Start();
Add Comment
Please, Sign In to add comment