我正在使用C#MVC 5.我已经建立了与活动目录的连接,并且我试图通过AD循环来通过html表单查找特定用户。 POST中的结果不是预期的结果。我得到了正确的AMOUNT(数量)搜索结果,但输出的结果相同。如下图所示:
2总结果是正确的,但也有一个Josh Barber而不仅仅是Josh VanDyke。 这是代码:
/**********Establish Connection********/
DirectoryEntry dir = createDirectoryEntry();
DirectorySearcher search = new DirectorySearcher(dir);
/****Refer to class constructor****/
ADUser userAttributes = new ADUser("", "", "", "", "", "", "");
/********Create the List to store results in***************/
List<ADUser> Users = new List<ADUser>();
string DisplayName = "", SAMAccountName = "", Mail = "", Description = "", Division = "", TelephoneNumber = "", FacsimileTelephoneNumber = "";
/*******Filter parameters************/
search.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(anr=" + userName + "* ))";
SearchResultCollection searchresult = search.FindAll();
search.PropertiesToLoad.Add("displayname");
search.PropertiesToLoad.Add("SAMAccountName");
search.PropertiesToLoad.Add("Mail");
search.PropertiesToLoad.Add("Description");
search.PropertiesToLoad.Add("TelephoneNumber");
search.PropertiesToLoad.Add("FacsimileTelephoneNumber");
search.PropertiesToLoad.Add("Division");
/*****************Filtering and populating the List****************/
if (searchresult != null)
{
foreach (SearchResult iResult in searchresult)
{
foreach (string PropertyName in iResult.Properties.PropertyNames)
{
foreach (Object key in iResult.GetDirectoryEntry().Properties[PropertyName])
{
try
{
switch (PropertyName.ToUpper())
{
case "DISPLAYNAME":
DisplayName = key.ToString();
userAttributes.Name = DisplayName;
break;
case "SAMACCOUNTNAME":
SAMAccountName = key.ToString();
userAttributes.DomainUserName = SAMAccountName;
break;
case "MAIL":
Mail = key.ToString();
userAttributes.EmailAddress = Mail;
break;
case "DESCRIPTION":
Description = key.ToString();
userAttributes.JobDescription = Description;
break;
case "TELEPHONENUMBER":
TelephoneNumber = key.ToString();
userAttributes.TelephoneNumber = TelephoneNumber;
break;
case "FACSIMILETELEPHONENUMBER":
FacsimileTelephoneNumber = key.ToString();
userAttributes.FaxNumber = FacsimileTelephoneNumber;
break;
case "DIVISION":
Division = key.ToString();
userAttributes.Division = Division;
break;
}
}
catch { }
}
}
Users.Add(userAttributes);
}
return View(Users);
}
return View();
}
答案 0 :(得分:2)
您不会重新创建或清理userAttributes
。
// not here
/****Refer to class constructor****/
// ADUser userAttributes = new ADUser("", "", "", "", "", "", "");
...
foreach (SearchResult iResult in searchresult)
{
// but here.
ADUser userAttributes = new ADUser("", "", "", "", "", "", "");
foreach (string PropertyName in iResult.Properties.PropertyNames)
{