Active Directory and asp .net

Mar 25 2008 5:49 PM
Hello,

I'm building a web app where I like to query my users from active directory.
The code I'm working on it works fine for a user where all the properties that I'm query are filled in.

I'm trying already for a week to check if a property is empty but I'm stuck.

I need a method how I can check if a property is empty so I can use a string like "property empty". after testing I bring the AD up-to-date.

I hope there are some specialist over here so I can find a solution for my problem.

Many thanks in advance.

Wim (starter)

Here under my code

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Collections.Generic;


using System.ComponentModel;

/// <summary>
/// Summary description for AdSearch
/// </summary>
namespace HelpdeskApplication
{
public class AdSearch
{
public AdSearch()
{
//
// TODO: Add constructor logic here
//
}
public static DirectoryEntry GetDirectoryEntry()
{
DirectoryEntry dirent = new DirectoryEntry();
dirent.Path = "LDAP://domain.local";

dirent.AuthenticationType = AuthenticationTypes.Secure;

return dirent;
}


public static List<adGetSet> GetAllADUsers()
{
DirectoryEntry adFolderObject = GetDirectoryEntry();
DirectorySearcher adSearcherObject = new DirectorySearcher(adFolderObject);
adSearcherObject.SearchScope = SearchScope.Subtree;
adSearcherObject.Filter = "(&(objectCategory=person)(objectClass=user)(!descrip tion=built-in*))";
adSearcherObject.PropertiesToLoad.Add("displayName");
adSearcherObject.PropertiesToLoad.Add("SAMAccountName");
adSearcherObject.PropertiesToLoad.Add("title");
adSearcherObject.PropertiesToLoad.Add("mail");
adSearcherObject.PropertiesToLoad.Add("department");
adSearcherObject.PropertiesToLoad.Add("manager");
adSearcherObject.PropertiesToLoad.Add("physicalDeliveryOfficeName");
adSearcherObject.PropertiesToLoad.Add("pager");
adSearcherObject.PropertiesToLoad.Add("homephone");
adSearcherObject.PropertiesToLoad.Add("facsimileTelephoneNumber");
adSearcherObject.PropertiesToLoad.Add("mobile");


List<adGetSet> adAllUsers = new List<adGetSet>();
adGetSet adUsers = new adGetSet();

foreach (SearchResult adObject in adSearcherObject.FindAll())
{

if (adObject.Properties["SAMAccountName"][0].ToString().Length == 5)
{
adUsers.ADUsername = adObject.Properties["displayName"][0].ToString();
adUsers.ADSAMAccountName = adObject.Properties["SAMAccountName"][0].ToString();

/// AD Title of user
/// check if the propertie is empty
if (string.IsNullOrEmpty(adObject.Properties["title"][0].ToString()))
{
adUsers.ADTitle = "noTitle";
}
else
{
adUsers.ADTitle = adObject.Properties["title"][0].ToString();
}
///End check AD Title of user

adUsers.ADMail = adObject.Properties["mail"][0].ToString();
adUsers.ADDepartment = adObject.Properties["department"][0].ToString();
adUsers.ADManager = adObject.Properties["manager"][0].ToString();
adUsers.ADDock = adObject.Properties["physicalDeliveryOfficeName"][0].ToString();
adUsers.ADPc = adObject.Properties["pager"][0].ToString();
adUsers.ADPhone = adObject.Properties["homephone"][0].ToString();
adUsers.ADFax = adObject.Properties["facsimileTelephoneNumber"][0].ToString();
adUsers.ADGsm = adObject.Properties["mobile"][0].ToString();
adAllUsers.Add(adUsers);
}
}
return adAllUsers;
}

}
}