如何解决;System.IO.IOException:用户名或密码不正确";使用asp.net时出错?

如何解决;System.IO.IOException:用户名或密码不正确";使用asp.net时出错?,asp.net,networking,directory,credentials,networkcredentials,Asp.net,Networking,Directory,Credentials,Networkcredentials,我只想得到文件夹名,它在不同的域中。当我尝试在本地获取文件夹名称时,我可以获取文件夹名称 这是我的密码 [WebMethod] public void getAllRootDirectoryNames(string path) { string userName = "Domain\\Admin"; string password = "Password"; NetworkCredential theNetworkCredential = new NetworkCrede

我只想得到文件夹名,它在不同的域中。当我尝试在本地获取文件夹名称时,我可以获取文件夹名称

这是我的密码

[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));
}
[WebMethod]
public void getAllRootDirectoryNames(字符串路径)
{
字符串userName=“域\\Admin”;
字符串password=“password”;
NetworkCredential theNetworkCredential=新的网络凭据(用户名、密码);
CredentialCache theNetcache=新的CredentialCache();
添加(新Uri(@“\\192.168.x.x”)、“基本”、网络凭据);
列表详细信息=新列表();
WriteLine(“开始获取所有根目录名”);
foreach(新DirectoryInfo(path).GetDirectories()中的var directoryName)
{
GetFolderDetails fd=新的GetFolderDetails();
fd.fullFolder=directoryName.Parent.Name;
fd.folderName=directoryName.Name;
fd.urlPath=path+directoryName.Name;
fd.subFolderExists=0;
详细信息。添加(fd);
}
JavaScriptSerializer js=新的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();

    }
[DllImport(“advapi32.dll”,SetLastError=true,CharSet=CharSet.Unicode)]
公共静态外部bool LogonUser(字符串lpszUsername、字符串lpszDomain、字符串lpszPassword、int-dwLogonType、int-dwLogonProvider、ref-IntPtr phToken);
[网络方法]
public void getAllRootDirectoryNames(字符串路径)
{
IntPtr令牌句柄=新的IntPtr(0);
tokenHandle=IntPtr.Zero;
bool returnValue=LogonUser(“用户名”、“域名”、“密码”,2,0,ref-tokenHandle);
WindowsIdentity ImpersonatedIdentity=新的WindowsIdentity(令牌句柄);
WindowsImpersonationContext MyImpersonation=ImpersonatedIdentity.Impersonate();
列表详细信息=新列表();
foreach(新DirectoryInfo(path).GetDirectories()中的var directoryName)
{
GetFolderDetails fd=新的GetFolderDetails();
fd.fullFolder=directoryName.Parent.Name;
fd.folderName=directoryName.Name;
//fd.urlPath=directoryName.FullName;
fd.urlPath=path+directoryName.Name;
fd.subFolderExists=0;
foreach(新DirectoryInfo(path+“/”+directoryName.Name+“/”).GetDirectories()中的var insideDirName)
{
fd.subFolderExists=1;
}
详细信息。添加(fd);
}
JavaScriptSerializer js=新的JavaScriptSerializer();
Context.Response.Write(js.Serialize(details));
MyImpersonation.Undo();
}
它抛出以下错误

“System.UnauthorizedAccessException”在mscorlib.dll中发生,但未在用户代码中处理


我想您的主机和目标计算机都是基于Windows的。我以前做过,但我的代码看起来有点不同。将尝试制作一些场景(简而言之)。 首先导入这个dll。检查参数并播放输入格式。我真的不记得它们应该是什么样子

[System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

public class TestClass
{
    public void TestMethod()
    {
        IntPtr admin_token = default(IntPtr);
        WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
        WindowsIdentity wid_admin = null;

        WindowsImpersonationContext wic = null;
        try

        {
            if (LogonUser(User, userDomain, Password, DwLogonType, DwLogonProvider, ref admin_token))
            {
                wid_admin = new WindowsIdentity(admin_token);
                wic = wid_admin.Impersonate();
                if (!Directory.Exists(@"C:\TempFiles")) Directory.CreateDirectory(@"C:\TempFiles");
                file.SaveAs(@"C:\TempFiles\" + fileName
                                             //+ GUID 
                                             + "");
            }
        }
        catch (Exception ex)
        {
            ...
        }
}
在这里,我将一些文件保存在另一个域中,但您可以检查代码以了解如何对其进行授权。

谢谢您的帮助。
iStream=newfilestream(filepath,FileMode.Open,FileAccess.Read,FileShare.Read)

与之不同。顺便说一句:这个空洞的口号太可怕了。甚至不适用于示例代码。当然,模拟和访问unc位置是不同的事情,但可以根据作者的需要提供一些想法。啊哈,同意使用异常处理,但这已经足够了,例如,它并没有要求任何实际使用:)@Liamneesan是的,我看到了你的更新。1.您的帐户(用户名/密码)应该可以访问目标主机。对此要有把握;2.尝试使用用户名格式,我不记得它应该是什么。可能是域名域名\\UserName@IuriiMaksimov我给出了我的
域\\用户名和密码
。但仍然不可能复制@UweKeim,我正在尝试one@UweKeim那么
NetworkCredential
有什么用呢?我可以知道为什么我会投反对票吗?