如何解决“系统.IO. IO异常:用户名或密码不正确”错误使用asp.net?

plupiseo  于 2023-03-20  发布在  .NET
关注(0)|答案(4)|浏览(552)

我只想获取其他域中的文件夹名称。当我尝试在本地获取文件夹名称时,可以获取文件夹名称。
下面是我的代码

[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

hlswsv35

hlswsv351#

我想你的主机和目标机器基于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)
        {
            ...
        }
}

这里我保存了一些文件在另一个域,但你可以检查代码,以获得如何使授权。

piztneat

piztneat3#

得到完全相同的错误信息,但不幸的是Yurii Maksimov的解决方案不为我工作;LogonUser总是返回false,无论我尝试什么。
然而,我确实在一个不同的堆栈溢出问题上找到了这个答案,它成功了,允许我通过需要凭据的UNC路径访问NAS上的文件。
张贴这一点,以防其他人遇到这个问题,并遇到同样的问题,我没有。

h6my8fg2

h6my8fg24#

如果您像我一样,正在运行一个以前可以运行但当前不能运行的程序,请确保您不像我一样,以管理员身份运行Visual Studio或程序本身。我忘记了我以管理员身份运行Visual Studio,单击“运行”开始调试,并收到此错误,因为管理员被用于访问网络驱动器,而不是域组具有访问权限的my用户。

相关问题