我需要通过检查指定域中是否存在这样的用户名来验证 LDAP 用户。为此,我正在使用此代码-
DirectoryEntry entry = new DirectoryEntry("LDAP://" + strDomainController);
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "SAMAccountName=" + strUserName;
SearchResult result = searcher.FindOne();
return (result != null) ? true : false;
这是类库中的一种方法,我打算在项目中需要此功能的任何地方引用和使用它。
为了测试这一点,我创建了一个简单的测试应用程序。测试是这样进行的 -
Console.WriteLine(MyClassLib.MyValidateUserMethod("UserName", "Domain",ref strError).ToString());
我面临的问题是,当我用我的 testapp 测试它时它工作正常,但在我的项目中,当我尝试使用具有相同凭据的相同方法时 -DirectoryEntry对象抛出"System.DirectoryServices.DirectoryServicesCOMException"异常并且search.Filter失败并抛出 ex = {"Logon失败:未知用户名或错误密码。\r\n"} 异常。
我尝试过模仿,但这无济于事。不知何故,相同的方法在 mytestapp 中运行良好,但在我的项目中不起作用。这两个应用程序都在我的本地开发机器中。我错过了什么?有任何想法吗?