Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 pre-populating form fields from Active Directory

Author  Topic 

-Dman100-
Posting Yak Master

210 Posts

Posted - 2007-06-06 : 16:55:42
I'm working on a registration form that allows users to register for our company's national sales conference. I am trying to pre-populate several of the form filelds from Active Directory making it easier for the user to fill out the form. I'm just returning basic information from Active Directory...first name, last name, job title, cost center, manager, address info, etc.

One of the problems I'm encountering is the data in Active Directory is not complete and not standardized.

For example, some people might be missing addresses, phone numbers, cost centers, managers, etc.

Also, the state information stored in Active Directory might use either an abbreviation for the state or the state might be spelled out...i.e. TX, or Texas.

This is presenting problems when trying to pre-populate the fields because if there is missing data in AD that I'm trying to pre-populate into the registration form, an exception is thrown because of null data.

Second, the dropdown list for the state list throws an error when it enounters a record in Active Directory that doesn't conform to the abbreviation standard we should be using.

I've tried to set the current selected item of the state list to what is stored in Active Directory. So, if user "A" is from TX or Tx, then I want to set the selected item in the state list to "Texas". However, if the record in Active Directory stores the state as "Texas" then an exception is thrown. I need to check for either case, which I'm having some trouble doing.

This has been my first attempt at working with data from Active Directory, so I am still struggling on how to handle this.

I created a class file that makes the connection to Active Directory.

Here is my method that opens the connection to Active Directory:

public DirectoryEntry GetDirectoryEntry(){
DirectoryEntry entry = new DirectoryEntry();
entry.Path = "LDAP://" + AD_SERVER + "/" + AD_BASEDN;
entry.AuthenticationType = AuthenticationTypes.Secure;
entry.Username = AD_AUTHORIZED_USER;
entry.Password = AD_AUTH_USER_PASSWD;
return entry;
}

Here is the method that returns the data from AD:

public Registration FindAccount(string account) {
Registration r = new Registration();
string missing_data_AD = null;
try {
anEntry= GetDirectoryEntry();
DirectorySearcher search = new DirectorySearcher(anEntry);
search.Filter = "(&(objectClass=user)(samaccountname="+account+"))";
search.PropertiesToLoad.Add("givenName");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("title");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("manager");
search.PropertiesToLoad.Add("telephoneNumber");
search.PropertiesToLoad.Add("st");
search.PropertiesToLoad.Add("l");
search.PropertiesToLoad.Add("postalCode");
search.PropertiesToLoad.Add("streetAddress");

SearchResult result = search.FindOne();

if (result != null){
if (result.Properties.Contains("givenname")) {
r.firstname = result.Properties["givenname"][0].ToString();
} else {
missing_data_AD = "givenname";
}

if (result.Properties.Contains("sn")) {
r.lastname = result.Properties["sn"][0].ToString();
} else {
missing_data_AD = missing_data_AD + ",sn";
}

if (result.Properties.Contains("title")) {
r.job_title = result.Properties["title"][0].ToString();
} else {
missing_data_AD = missing_data_AD + ",title";
}

if (result.Properties.Contains("mail")) {
r.email = result.Properties["mail"][0].ToString();
} else {
missing_data_AD = missing_data_AD + ",mail";
}

if (result.Properties.Contains("manager")) {
r.manager = result.Properties["manager"][0].ToString();
} else {
missing_data_AD = missing_data_AD + ",manager";
}

State s = new State();

if (result.Properties.Contains("telephoneNumber")) {
r.phone = result.Properties["telephoneNumber"][0].ToString();
} else {
missing_data_AD = missing_data_AD + ",telephoneNumber";
}

if (result.Properties.Contains("st")) {
s.Name = result.Properties["st"][0].ToString();
} else {
missing_data_AD = missing_data_AD + ",st";
}
r.state = s;

if (result.Properties.Contains("l")) {
r.city = result.Properties["l"][0].ToString();
} else {
missing_data_AD = missing_data_AD + ",l";
}

if (result.Properties.Contains("postalCode")) {
r.zip = result.Properties["postalCode"][0].ToString();
} else {
missing_data_AD = missing_data_AD + ",postalCode";
}

if (result.Properties.Contains("streetAddress")) {
r.address = result.Properties["streetAddress"][0].ToString();
} else {
missing_data_AD = missing_data_AD + ",streetAddress";
}


if (missing_data_AD == null)
missing_data_AD = "ok";

r.missingDataAD = missing_data_AD;
}else{
//return null;
}

return r;
}
catch (Exception ex){
ErrorAndException.last_exception_msg=ex.ToString();
return null;
}
}

The Registration object is the container for the data and holds all the getter and setter methods.

First, is there a better way to open my connection to AD?

Second, is there a better way to return the data from AD and how can I prevent the issue of null values throwing errors when there is missing data in AD that I'm returning?

Third, how can I handle if the state is either an abbreviation or spelled out? That has been tricky?

Please let me know if I need to provide more information or additional code snippets from my code-behind or class files?

Thanks in advance for any help.
   

- Advertisement -