【问题标题】:How to solve "System.IO.IOException: The user name or password is incorrect" error using asp.net?使用asp.net如何解决“System.IO.IOException:用户名或密码不正确”错误?
【发布时间】:2018-10-16 07:37:40
【问题描述】:

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

这是我的代码

[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

【问题讨论】:

标签: asp.net networking directory credentials networkcredentials


【解决方案1】:

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

这里我将一些文件保存在另一个域中,但是您可以检查代码以了解如何对其进行授权。

【讨论】:

  • 顺便说一句:这个空洞的流行语太可怕了。甚至不适合示例代码。最好不要提供所有过早的异常处理,而不是提供错误的处理。
  • 当然,模拟和访问 unc 位置是不同的事情,但可以为作者的需求提供一些想法。啊哈,同意异常处理,但是例如就足够了,并且没有要求任何实际使用:)
  • @Liamneesan 是的,我看到了你的更新。 1. 您的帐户(用户名/密码)应该可以访问您的目标主机。确定这一点; 2.尝试使用用户名格式,我不记得确切应该是什么。也许与 domainName DomainName\\UserName
  • @IuriiMaksimov 我给了我的Domain\\Username and Password。但仍然没有
【解决方案2】:

谢谢它的工作。 iStream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);

【讨论】:

    【解决方案3】:

    收到完全相同的错误消息,但不幸的是 Yurii Maksimov 的解决方案对我不起作用;无论我尝试什么,LogonUser 总是返回 false

    但是,我确实找到了 this answer on a different Stack Overflow question which did the trick,并允许我通过需要凭据的 UNC 路径访问 NAS 上的文件。

    发布此内容以防其他人遇到此问题并遇到与我相同的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      • 2018-11-20
      • 1970-01-01
      • 2016-12-02
      • 2012-05-14
      • 2017-01-06
      相关资源
      最近更新 更多