Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Active directory in ASP.net

Status
Not open for further replies.

arbo80

Programmer
Jan 5, 2006
53
0
0
US
Hi all,

I have the following code to read user information (email address) from Active Directory in ASP.net. However, I'm getting this error message: System.Runtime.InteropServices.COMException: The server is not operational. Has anybody gotten the same error? I need your help.

string filter = string.Format("(&ObjectClass={0}) (sAMAccountName={1}))", "person", principal);
string domain = "DOMAIN";
string[] properties = new string[] { "fullname" };

DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domain, null, null, AuthenticationTypes.Secure);
DirectorySearcher searcher = new DirectorySearcher(adRoot);
searcher.SearchScope= SearchScope.Subtree;
searcher.ReferralChasing= ReferralChasingOption.All;
searcher.PropertiesToLoad.AddRange(properties);
searcher.Filter= filter;

SearchResult result = searcher.FindOne();

DirectoryEntry directoryEntry = result.GetDirectoryEntry();


string displayName = directoryEntry.Properties["displayName"][0].ToString();
string email = directoryEntry.Properties["mail"][0].ToString();

Label1.Text= displayName;
Label2.Text = email;

}

Thanks,
A.
 
I forgot this line at the top:

string principal = this.Context.User.Identity.Name;
 
what line does it error on?
my guess is
Code:
DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domain, null, null, AuthenticationTypes.Secure);
null and null refer to the user id and password correct?
if so they need to be supplied. I don't think you cannot, not, pass these values.

some other items to note:
you are loading the property fullname, but want to retrieve mail and displayname. I think this will cause problems once you solve the current issue.

also, Context.User.Identity.Name returns DOMAIN\username. you will probably need to parse the DOMAIN\ out of the name.

to remove the dependency from asp.net you can use System.Threading.Thread.Current.Identity (or Principle). either way, you can get away from the asp.net framework.

since your doing a simple fetch of properties (no hierarchical searches or anything. you can use ADO.Net to query AD. I believe it's faster. no stats to back that up. with this approach I can treat it like any other "database" and use all the ado.net abstractions.
two things to note with this approach.
1. you cannot use transactions
2. you cannot use parameters. you must use injected sql :(


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks! I will try your suggestions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top