asp.net implementation of access to the local area network shared directory file Solution _ Practical Skills

Source: Internet
Author: User

This article is an example of the ASP.net implementation access to the local area network shared directory of files under the solution, the complete code is as follows:

Using System; 
Using System.Collections; 
Using System.Configuration; 
Using System.Data; 
Using System.Linq; 
Using System.Web; 
Using System.Web.Security; 
Using System.Web.UI; 
Using System.Web.UI.HtmlControls; 
Using System.Web.UI.WebControls; 
Using System.Web.UI.WebControls.WebParts; 
Using System.Xml.Linq; 
Using System.IO; 
Using System.Security.Principal; 
Using System.Runtime.InteropServices; 
  Public partial class _default:system.web.ui.page {public const int logon32_logon_interactive = 2; 
  public const int logon32_provider_default = 0; 
  WindowsImpersonationContext impersonationcontext; [DllImport ("advapi32.dll")] public static extern int Logonusera (String lpszUserName, String lpszdomain, Strin 
  G Lpszpassword, int dwlogontype, int dwlogonprovider, ref IntPtr phtoken);  [DllImport ("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern int DuplicateToken (INTPTR htoken, int impersonationlevel, REF IntPtr hNewToken); 
  [DllImport ("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool RevertToSelf (); 
  [DllImport ("kernel32.dll", CharSet = CharSet.Auto)] public static extern bool CloseHandle (INTPTR handle); 
      public void Page_Load (Object s, EventArgs e) {if (impersonateValidUser ("Lucas", "Workgroup", "Lcas")) { 
      string path = @ "//zhehui001/lu"; 
      foreach (string f in Directory.GetFiles (path)) {Response.Write (f); 
    } undoimpersonation (); else {//your impersonation failed. 
    Therefore, include a fail-safe mechanism here. } private bool impersonateValidUser (string userName, string domain, string password) {WindowsIdentity tem 
    pwindowsidentity; 
    IntPtr token = IntPtr.Zero; 
    IntPtr tokenduplicate = IntPtr.Zero; if (RevertToSelf ()) {if Logonusera (userName, domain, password, logon32_logon_interactive, logon32_p RoVider_default, ref token)!= 0) {if (DuplicateToken (token, 2, ref tokenduplicate)!= 0) { 
          tempWindowsIdentity = new WindowsIdentity (tokenduplicate); 
          impersonationcontext = Tempwindowsidentity.impersonate (); 
            if (impersonationcontext!= null) {CloseHandle (token); 
            CloseHandle (tokenduplicate); 
          return true; 
    {}}} if (token!= intptr.zero) CloseHandle (token); 
    if (tokenduplicate!= IntPtr.Zero) CloseHandle (tokenduplicate); 
  return false; 
  private void Undoimpersonation () {Impersonationcontext.undo (); } 
}

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.