Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Configuration;
- using System.Diagnostics;
- using System.Text.RegularExpressions;
- using System.Security;
- using System.Net;
- using System.Security.Principal;
- using System.Reflection;
- using System.IO;
- public static void SetAlternateUser(string userName, string password, string domain)
- {
- // thread safe singleton code
- lock (threadSafeLock)
- {
- if (_ie == null)
- {
- StartIE();
- }
- _ie.Close();
- _ie = null;
- // fill the NetworkCredeitials object that we use for impersonation
- if (string.IsNullOrEmpty(domain))
- {
- alternateUserCredentials = new NetworkCredential(userName, password);
- }
- else
- {
- alternateUserCredentials = new NetworkCredential(userName, password, domain);
- }
- // Prepare to launch
- ProcessStartInfo psi = new ProcessStartInfo();
- psi.UserName = userName;
- psi.Password = SecurePassword(password);
- if (!string.IsNullOrEmpty(domain))
- {
- psi.Domain = domain;
- }
- psi.UseShellExecute = false;
- psi.LoadUserProfile = true;
- psi.FileName = "c:\Program Files\Internet Explorer\iexplore.exe";
- psi.Arguments = "about:blank";
- // launch
- Process proc = new Process();
- proc.StartInfo = psi;
- proc.Start();
- // Time to become an imposter
- hToken = IntPtr.Zero;
- hTokenDuplicate = IntPtr.Zero;
- if (Win32.LogonUser(alternateUserCredentials.UserName, alternateUserCredentials.Domain, alternateUserCredentials.Password, 2 /*LOGON32_LOGON_INTERACTIVE*/, 0 /*LOGON32_PROVIDER_DEFAULT*/, out hToken))
- {
- if (Win32.DuplicateToken(hToken, 2, out hTokenDuplicate))
- {
- windowsIdentity = new WindowsIdentity(hTokenDuplicate);
- impersonationContext = windowsIdentity.Impersonate();
- System.Threading.Thread.Sleep(1000);
- _ie = IE.AttachToIE(Find.ByUrl("about:blank"));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement