title: How to solve "System.IO.IOException: The user name or password is incorrect" error using asp.net?
body: <p>I just want to get the folder name which is in a different domain. I can get the folder name when I try to get the folder name locally.</p>
  
  <p>Here is my code</p>
  
  <pre><code>[WebMethod]
  public void getAllRootDirectoryNames(string path)
  {
      string userName = "Domain\\Admin";
      string password = "Password";
      NetworkCredential theNetworkCredential = new NetworkCredential(userName, password);
      CredentialCache theNetcache = new CredentialCache();
  
      theNetcache.Add(new Uri(@"\\192.168.x.x"), "Basic", theNetworkCredential);
  
      List&lt;GetFolderDetails&gt; details = new List&lt;GetFolderDetails&gt;();
      Debug.WriteLine("GET All Root Directory Names START");
  
      foreach (var directoryName in new DirectoryInfo(path).GetDirectories())
      {
          GetFolderDetails fd = new GetFolderDetails();
          fd.fullFolder = directoryName.Parent.Name;
          fd.folderName = directoryName.Name;
  
          fd.urlPath = path + directoryName.Name;
          fd.subFolderExists = 0;
  
          details.Add(fd);
      }
  
      JavaScriptSerializer js = new JavaScriptSerializer();
      Context.Response.Write(js.Serialize(details));
  }
  </code></pre>
  
  <p>Error message:</p>
  
  <blockquote>
    <p>System.IO.IOException: The user name or password is incorrect.</p>
  </blockquote>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I tried this below code.</p>
  
  <pre><code>[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
      public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
  
      [WebMethod]
      public void getAllRootDirectoryNames(string path)
      {
  
          IntPtr tokenHandle = new IntPtr(0);
          tokenHandle = IntPtr.Zero;
  
          bool returnValue = LogonUser("USerName", "DomainName", "password", 2, 0, ref tokenHandle);
          WindowsIdentity ImpersonatedIdentity = new WindowsIdentity(tokenHandle);
          WindowsImpersonationContext MyImpersonation = ImpersonatedIdentity.Impersonate();
  
  
          List&lt;GetFolderDetails&gt; details = new List&lt;GetFolderDetails&gt;();
  
          foreach (var directoryName in new DirectoryInfo(path).GetDirectories())
          {
              GetFolderDetails fd = new GetFolderDetails();
              fd.fullFolder = directoryName.Parent.Name;
              fd.folderName = directoryName.Name;
              //fd.urlPath = directoryName.FullName;
              fd.urlPath = path + directoryName.Name;
              fd.subFolderExists = 0;
  
  
              foreach (var insideDirName in new DirectoryInfo(path + "/" + directoryName.Name + "/").GetDirectories())
              {
                  fd.subFolderExists = 1;
              }
              details.Add(fd);
          }
          JavaScriptSerializer js = new JavaScriptSerializer();
          Context.Response.Write(js.Serialize(details));
  
          MyImpersonation.Undo();
  
      }
  </code></pre>
  
  <p><strong>It throws the following error</strong></p>
  
  <blockquote>
    <p><code>'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code</code></p>
  </blockquote>