Public classidentityscope:idisposable {/// <summary> ///sign in to a new user/// </summary> /// <param name= "Pszusername" ></param> /// <param name= "Pszdomain" ></param> /// <param name= "Pszpassword" ></param> /// <param name= "dwLogonType" ></param> /// <param name= "dwLogonProvider" ></param> /// <param name= "Phtoken" ></param> /// <returns></returns>[DllImport ("Advapi32.dll", SetLastError =true)] Static extern BOOLLogonUser (stringPszusername,stringPszdomain,stringPszpassword,intdwLogonType,intdwLogonProvider,refIntPtr Phtoken); //closes open handes returned by LogonUser[DllImport ("Kernel32.dll", CharSet =CharSet.Auto)]extern Static BOOLCloseHandle (IntPtr handle); /// <summary> ///let the calling thread impersonate a user/// </summary> /// <param name= "Htoken" ></param> /// <returns></returns>[DllImport ("Advapi32.DLL")] Static extern BOOLImpersonateLoggedOnUser (IntPtr htoken); /// <summary> ///Terminate a simulated client application/// </summary> /// <returns></returns>[DllImport ("Advapi32.DLL")] Static extern BOOLRevertToSelf (); PublicIdentityscope (stringUsername,stringDomain,stringPassword) { //Initialize TokensIntPtr Pexistingtokenhandle =NewINTPTR (0); INTPTR Pduplicatetokenhandle=NewINTPTR (0); Try { //get handle to token BOOLbimpersonated = LogonUser (Username, Domain, Password, Logon32_logon_newcredentials, Logon32_provider_default,refpexistingtokenhandle); if(true==bimpersonated) { if(!ImpersonateLoggedOnUser (Pexistingtokenhandle)) { intNerrorcode =Marshal.GetLastWin32Error (); Throw NewException ("ImpersonateLoggedOnUser error; Code="+Nerrorcode); } } Else { intNerrorcode =Marshal.GetLastWin32Error (); Throw NewException ("LogonUser error; Code="+Nerrorcode); } } finally { //Close handle (s) if(Pexistingtokenhandle! =IntPtr.Zero) CloseHandle (Pexistingtokenhandle); if(Pduplicatetokenhandle! =IntPtr.Zero) CloseHandle (Pduplicatetokenhandle); } } Const intLogon32_provider_default =0; Const intLogon32_logon_newcredentials =9; Private BOOLdisposed; Public voidDispose () {Dispose (true); } protected Virtual voidDispose (BOOLdisposing) { if(!disposed) {RevertToSelf (); Disposed=true; } } }
using New Identityscope (" logon Server user name "" server IP" " Login server Password" ) { // after connecting to the server, do what you want by doing the normal operation! }
. Net, fetch the files on the server