登录失败:C#中的未知用户名或密码错误

时间:2015-07-20 08:42:41

标签: c# vb.net active-directory ldap

某些计算机无法连接到我们的活动目录服务器。我不知道我的代码是否存在问题导致其工作到其他PC。我正在使用.net framework 4.0。所有PC都连接到同一网络。它可以ping我们的AD服务器。请帮忙!

public bool CheckADAccount(string strUserAccount, bool Stat)
    {
        try {
            DirectoryEntry rootDirEntry = new DirectoryEntry("LDAP://" + dbADServer + "/CN=Users," + "DC=companyname,DC=net,DC=ph", dbADUser, dbADPass);
            DirectorySearcher DirSearcher = new DirectorySearcher(rootDirEntry, "(&(objectClass=User)(sAMAccountName=" + strUserAccount + "))");

            DirSearcher.PageSize = 5;
            DirSearcher.ServerTimeLimit = new TimeSpan(0, 0, 30);
            DirSearcher.ClientTimeout = new TimeSpan(0, 10, 0);
            DirSearcher.PropertiesToLoad.Add("cn");

            SearchResultCollection SearchResultCol = DirSearcher.FindAll();

            foreach (SearchResult Result in SearchResultCol)
            {
                adAccountName = Result.Properties["cn"][0].ToString();
                Stat = true;
            }
        }
        catch(Exception ex) {
            MessageBox.Show(ex.Message, "System Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning);  
            Stat = false;
        }
        return Stat;
    }

0 个答案:

没有答案