我只想获取位于不同域中的文件夹名称。当我尝试在本地获取文件夹名称时,我可以获得文件夹名称。
这是我的代码
[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<GetFolderDetails> details = new List<GetFolderDetails>();
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));
}
错误信息:
System.IO.IOException:用户名或密码不正确。
更新
我在下面的代码中尝试了这个。
[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<GetFolderDetails> details = new List<GetFolderDetails>();
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();
}
它抛出以下错误
'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code