Useful Link http://www.codeproject.com/KB/system/QueryADwithDotNet.aspx
On Thu, Mar 26, 2009 at 2:27 PM, Dhananjay <[email protected]> wrote: > > 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; > } > > > >
