Here is my code that used name to find users in AD. It returns a dt back
for a listbox. A user name was put into a textbox and we returned all users
from AD.
System.DirectoryServices.DirectoryEntry entry =
new System.DirectoryServices.DirectoryEntry(LDAPuri);
System.DirectoryServices.DirectorySearcher mySearcher = new
System.DirectoryServices.DirectorySearcher(entry);
mySearcher.Filter = ("(anr= " + name + ")");
mySearcher.PropertiesToLoad.Add("sn");
mySearcher.PropertiesToLoad.Add("givenName");
mySearcher.PropertiesToLoad.Add("userPrincipalName");
mySearcher.PropertiesToLoad.Add("samAccountName");
mySearcher.PropertiesToLoad.Add("mail");
mySearcher.PropertiesToLoad.Add("userAccountControl");
foreach (System.DirectoryServices.SearchResult result in
mySearcher.FindAll())
{
if (i > 0)
dr = dt.NewRow();
try
{
dr["uName"] =
result.GetDirectoryEntry().Properties["Name"][0].ToString(); // Name
dr["uEmail"] =
result.GetDirectoryEntry().Properties["mail"].Value.ToString();
dr["uEmpID"] =
result.GetDirectoryEntry().Properties["samAccountName"].Value.ToString();
i++;
}
catch { }
}
return dt;
.........................
Stephen Russell -
Senior Visual Studio Developer, DBA
Memphis, TN
901.246-0159
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Dhananjay
> Sent: Thursday, March 26, 2009 3:58 AM
> To: DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web
> Services,.NET Remoting
> Subject: [DotNetDevelopment] problem in this block of code provided can
> anyone correct thisblock of code
>
>
> hi all
>
> I am facing problem in this block of code provided below can you
> please help me to get solved! the problem gets executed when foreach
> block of code gets executed to get a list of items and values from the
> Active Directory list. It doesn't return anything . it should return
> the userIfo with values .Please help me to resolve this issue.
>
> Thanks in advance !
>
>
>
> =======================================================================
> ===============================
>
> private static System.Collections.Specialized.NameValueCollection
> userPropertyKeys = null;
> public User GetUserInfoFromAD(string username)
> {
>
> User userInfo = new User();
>
> string str = username;
> DirectoryEntry myldapconnection = createDirectoryEntry();
>
> DirectorySearcher search = new DirectorySearcher(myldapconnection);
> search.Filter = "(cn=" + str + ")";
>
> SearchResult result = search.FindOne();
>
>
> foreach (string ldapfield in fields.PropertyNames)
> {
>
> if (fields != null && userPropertyKeys.Keys.Count > 0)
> {
>
> for (int i = 0; i < userPropertyKeys.Keys.Count - 1; i++)
> {
>
> string userPropertyValue = userPropertyKeys.Keys[i];if (!
> string.IsNullOrEmpty(userPropertyValue) && result.Properties
> [userPropertyValue].Count > 0)
> {
>
> switch (userPropertyKeys.Keys[i])
> {
>
> case "name":
>
> if (!userInfo.UserName.Equals(result.Properties[userPropertyValue]
> [0].ToString()))
> {
>
> //userInfo = true;
>
> userInfo.UserName = result.Properties[userPropertyValue][0].ToString
> ();
>
> }
>
> break;
> case "Password":
>
> if (!userInfo.Password.Equals(result.Properties[userPropertyValue]
> [0].ToString()))
> {
>
> //userInfo = true;
>
> userInfo.Password = result.Properties[userPropertyValue][0].ToString
> ();
>
> }
>
> break;
> case "firstname":
>
> if (!userInfo.FirstName.Equals(result.Properties[userPropertyValue]
> [0].ToString()))
> {
>
> //username = true;
>
> userInfo.FirstName = result.Properties[userPropertyValue][0].ToString
> ();
>
> }
>
> break;
> case "lastname":
>
> if (!userInfo.LastName.Equals(result.Properties[userPropertyValue]
> [0].ToString()))
> {
>
> //username = true;
>
> userInfo.LastName = result.Properties[userPropertyValue][0].ToString
> ();
>
> }
>
> break;
> case "displayName":
>
> if (!userInfo.LoginName.Equals(result.Properties[userPropertyValue]
> [0].ToString()))
> {
>
> //username = true;
>
> userInfo.LoginName = result.Properties[userPropertyValue][0].ToString
> ();
>
> }
>
> break;
> case "country":
>
> if (!userInfo.Country.Equals(result.Properties[userPropertyValue]
> [0].ToString()))
> {
>
> //username = true;
>
> userInfo.Country = result.Properties[userPropertyValue][0].ToString();
>
> }
>
> break;
> case "state":
>
> if (!userInfo.State.Equals(result.Properties[userPropertyValue]
> [0].ToString()))
> {
>
> //username = true;
>
> userInfo.State = result.Properties[userPropertyValue][0].ToString();
>
> }
>
> break;
> case "zip":
>
> if (!userInfo.Zip.Equals(result.Properties[userPropertyValue]
> [0].ToString()))
> {
>
> //username = true;
>
> userInfo.Zip = result.Properties[userPropertyValue][0].ToString();
>
> }
>
> break;
> case "mail":
>
> if (!userInfo.Email.Equals(result.Properties[userPropertyValue]
> [0].ToString()))
> {
>
> //username = true;
>
> userInfo.Email = result.Properties[userPropertyValue][0].ToString();
>
> }
>
> break;
> case "telephoneNumber":
>
> if (!userInfo.OfficePhone.Equals(result.Properties[userPropertyValue]
> [0].ToString()))
> {
>
> //username = true;
>
> userInfo.OfficePhone = result.Properties[userPropertyValue][0].ToString
> ();
>
> }
>
> break;
> case "mobilePhone":
>
> if (!userInfo.MobilePhone.Equals(result.Properties[userPropertyValue]
> [0].ToString()))
> {
>
> //username = true;
>
> userInfo.MobilePhone = result.Properties[userPropertyValue][0].ToString
> ();
>
> }
>
> break;
> case "mobileCareer":
>
> if (!userInfo.MobileCarrier.Equals(result.Properties[userPropertyValue]
> [0].ToString()))
> {
>
> //username = true;
>
> userInfo.MobileCarrier = result.Properties[userPropertyValue]
> [0].ToString();
>
> }
>
> break;
> case "active":
>
> if (!userInfo.Active.Equals(result.Properties[userPropertyValue]
> [0].ToString()))
> {
>
> //username = true;
>
> userInfo.Active = (int)result.Properties[userPropertyValue][0];
> }
>
> break;
> case "creation_date":
>
> if (!userInfo.CreationDate.Equals(result.Properties[userPropertyValue]
> [0].ToString()))
> {
>
> //username = true;
>
> userInfo.CreationDate = (DateTime)result.Properties[userPropertyValue]
> [0];
> }
>
> break;
> }
>
> }
>
> }
>
>
>
> }
>
>
> }return userInfo;
> }
>
>
>
>
> No virus found in this incoming message.
> Checked by AVG - www.avg.com
> Version: 8.0.238 / Virus Database: 270.11.28/2022 - Release Date:
> 03/25/09 07:16:00