asp.net access Network path method (impersonate user login) _php tips

Source: Internet
Author: User

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? &LT;/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");
}

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.