. Net, fetch the files on the server

Source: Internet
Author: User

  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.