0
好吧,我已經連接到AD服務器,並創建了用戶,沒有任何問題。但是,當我嘗試獲取用戶列表時,它總是失敗。C#嘗試獲取用戶列表時的LDAP訪問衝突
我可以使用LDAP://domain.com,「user」,「password」進行連接,但當我嘗試其他任何操作時,會出現訪問衝突。 EX:LDAP:// CN = Users,DC = domain,DC = com或LDAP://domain.com/CN=Users,DC=domain,DC=com返回訪問衝突異常。如果我通過根進行搜索並獲取路徑,並使用路徑創建新條目,則會出現訪問衝突。
看來我只能連接到根。我不知道如何從那裏獲取用戶列表。誰能幫忙?
編輯: 這是我最終合作的代碼。它以我想要的方式工作。如果有人遇到這個問題,我會把它留在這裏。我在代碼中添加了對錯誤彈出的註釋。
private void List_Logon()
{
DirectoryEntry Entry = new DirectoryEntry("LDAP://domain.com", "user", "password", AuthenticationTypes.Secure);
DirectorySearcher searcher = new DirectorySearcher(Entry);
searcher.Filter = "(&(objectCategory=person)(objectClass=user))";
SearchResultCollection results = searcher.FindAll();
//I had to add null checks. This was never a problem, but should be checked.
if (results != null)
{
//For some odd reason, I couldn't use a foreach here.
for (int i = 0; i < results.Count; i++)
{
try
{
DirectoryEntry user = results[i].GetDirectoryEntry();
//This information cannot return null or it will throw an Access Violation Exception.
if (user.Properties["Name"].Value != null & user.Properties["lastLogon"].Value != null)
{
//This part is not mine. Someone else told me how to convert it.
Int64 lastlogon = new Int64();
IADsLargeInteger lgInt =
(IADsLargeInteger)user.Properties["lastLogon"].Value;
lastlogon = ((long)lgInt.HighPart << 32) + lgInt.LowPart;
DateTime lastLogonTime = DateTime.FromFileTime(lastlogon);
//Thank you.
listBox1.Items.Add(user.Properties["Name"].Value.ToString() + " : " + lastLogonTime.ToString());
}
//If one of the above is null, I still want to try to get the user.
else if (user.Properties["Name"].Value != null)
{
listBox1.Items.Add(user.Properties["Name"].Value.ToString());
}
else
{
//Don't believe this will ever happen, but it's possible... I think. I'm still learning.
listBox1.Items.Add("result was null");
}
}
catch (Exception e)
{
//This is how I found out what was really wrong. Catching the Access Violation Exception did not work. This was returning a Null Reference message.
MessageBox.Show(e.Message(), "Error");
}
}
}
}
真的沒有人可以幫助嗎? –
您確定您綁定到Active Directory的用戶具有查詢目錄的權限嗎?請查閱這篇關於允許這些類型的查詢的知識庫文章:https://support.microsoft.com/en-us/kb/320528 – X3074861X
也許您可以發佈代碼和關聯的異常? –