Core code:
public class Identityscope:idisposable {//obtains user token [DllImport ("advapi32.dll", SetLastError = True)] static extern bool LogonUser (string pszusername, String pszdomain, String pszpassword,int dwlogontype, int dwlogonpro
Vider, ref IntPtr phtoken); Closes open handes returned by LogonUser [DllImport ("kernel32.dll", CharSet = CharSet.Auto)] extern static bool
CloseHandle (IntPtr handle);
[DllImport ("Advapi32.DLL")] static extern bool ImpersonateLoggedOnUser (IntPtr htoken);
[DllImport ("Advapi32.DLL")] static extern bool RevertToSelf ();
const int logon32_provider_default = 0; const int logon32_logon_newcredentials = 9;//domain driving control? Need è to use?: I
Nteractive = 2 private bool disposed;
<summary>///on the record? </summary>///<param name= "sUserName" > with the User § name?</param>///"<param name= sdomain" > Domain driving name?? In the Ú domain driving D is í make 1 with the machine Ú ÷IP site </param>///<param name= "Spassword" > Midi u code? </PARam> public Identityscope (String susername, String Sdomain, String spassword) {//Initialize tokens
IntPtr pexistingtokenhandle = new IntPtr (0);
IntPtr pduplicatetokenhandle = new IntPtr (0); try {//Get handle to token bool bimpersonated = LogonUser (sUserName, Sdomain, Spassword,logon32_lo
Gon_newcredentials, Logon32_provider_default, ref pexistingtokenhandle); if (true = = bimpersonated) {if (!
ImpersonateLoggedOnUser (Pexistingtokenhandle)) {int nerrorcode = Marshal.GetLastWin32Error (); throw new Exception ("ImpersonateLoggedOnUser error;
Code= "+ nerrorcode);
} else {int nerrorcode = Marshal.GetLastWin32Error (); throw new Exception ("LogonUser error;
Code= "+ nerrorcode); } finally {//close handle (s) if (Pexistingtokenhandle!= IntPtr.Zero) Clos Ehandle (PexistinGtokenhandle);
if (pduplicatetokenhandle!= IntPtr.Zero) CloseHandle (Pduplicatetokenhandle);
} protected virtual void Dispose (bool disposing) {if (!disposed) {reverttoself ();
disposed = true;
}} public void Dispose () {Dispose (true); }
}
The second parameter is the domain name, there is a domain name, write the domain name, no domain name to write the target machine IP on it
using (identityscope c = new Identityscope ("Administrator", "192.168.0.1", "11111"))
{
string[] filelist = System.IO.Directory.GetDirectories (@ "\\192.168.0.1\folderName");
}