我需要一点帮助。我是活动目录的新手。我想用c#连接我的活动目录。这是我写的示例代码。
public void GetConnection()
{
var username = "xxxx";
var domain = "xxxx";
var password = "xxxx";
var path = "LDAP://xxxx/CN=xx";
DirectoryEntry de = new DirectoryEntry(sDomain + "/" + sDefaultOU, sUsername, sServicePassword, AuthenticationTypes.ServerBind);
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "(&(objectClass=Users))";
var sr = ds.FindAll();
if (sr != null)
{
MessageBox.Show("success");
}
else
{
MessageBox.Show("error");
}
}
}
附近有一个COMException was unhandled
var sr = ds.FindAll();
错误是:
未处理的类型' System.Runtime.InteropServices.COMException'发生在System.DirectoryServices.dll
中其他信息:未指定的错误
我可以帮助解决问题吗?提前致谢
答案 0 :(得分:0)
第1步
static DirectoryEntry createDirectoryEntry()
{
// create and return new LDAP connection with desired settings
//This is for ssl secure port for non secure port just make 636 as 389 and //change Authentication as None
DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://mysystem.domain.com:636","Admin","Domain123",AuthenticationTypes.SecureSocketsLayer);
return ldapConnection;
}
**
** 在主要功能中,您需要编写以下功能
DirectorySearcher _searcher = null;
SearchResult result_user = null;
DirectoryEntry de = createDirectoryEntry();
object o = de.SchemaEntry;
_searcher = new DirectorySearcher(de, "(&(objectClass=user)(SAMAccountName=" + "user1" + "))");
if (_searcher != null)
{
result_user = _searcher.FindOne();
de.Close();
}