在本網站的答案線索中找到了文檔(here),但我無法獲得與AD的連接。當我使用像Active Directory Explorer這樣的程序時,我可以連接。我想,因爲我試圖連接到一個LDAPS,我需要一個不同的方法?如何通過C#中的LDAPS連接到Active Directory?
我有服務器的IP,域名,用戶名/密碼和端口636. 我試過各種組合@new DirectoryEntry
,但無法獲取它連接。總是得到一個COMException Domain is not existing
。
static DirectoryEntry createDirectoryEntry()
{
DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://192.168.2.59", USER, PWD);
ldapConnection.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;
return ldapConnection;
}
背景的相關信息: 用戶將他卡讀卡器單位。 Porgram從卡上獲取ID並在數據庫中搜索該ID並返回屬於該ID /用戶 的電子郵件地址。 在這裏工作的解決方案:
private string getEmail(string userID)
{
try
{
string ldapfilter = "(&(otherPager=" + userID + "))";
DirectoryEntry myLdapConnection = new DirectoryEntry("LDAP://" + SERVER, USER, PWD);
DirectorySearcher search = new DirectorySearcher(myLdapConnection);
search.Filter = ldapfilter;
/*search.PropertiesToLoad.Add("mail");
SearchResult result = search.FindOne();*/
string[] requiredValue = new String[] { "mail" };
foreach (String value in requiredValue)
search.PropertiesToLoad.Add(value);
SearchResult result = search.FindOne();
if (result != null)
{
foreach (String value in requiredValue)
foreach (Object myCollection in result.Properties[value])
{
return myCollection.ToString();
}
}
else
{
return "No Entry fround";
}
}
catch (Exception e)
{
Console.WriteLine("Exception Problem: " + e.ToString());
return null;
}
return null;
}
private void cmdClose_Click(object sender, EventArgs e)
{
Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
label1.Text = getEmail(textBox1.Text);
}
嘗試更像「LDAPS://:636/DC = example,DC = com」 –
jwilleke