Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi . Az you know every user that is defined in active directory has FirstName,LastName ,UserLoginName(sAMAccountName),Email,.... now i have FirstName+LastName of any users on my local network and i want to get UserLoginName of any users from server's active directory.what can i do?? and here my code::

C#
DirectoryServices.SearchResult myResult;
string filterString = string.Empty;
string EntryString  = "LDAP:// MyDomain";

DirectoryServices.DirectorySearcher myDirectorySearcher = new DirectoryServices.DirectorySearcher(new DirectoryServices.DirectoryEntry(EntryString));
string tempStr;
string[] splStr = new string[3];

filterString               = "CN="+FisrtAndLastNameOfUser;
myDirectorySearcher.Filter = filterString;
myDirectorySearcher.PropertiesToLoad.Add("UserName");
myResult    = myDirectorySearcher.FindOne();
splStr      = Regex.Split(myResult.Properties("UserName").Item(0).ToString, " ");
tempStr     = splStr(1).ToString + " " + splStr(0).ToString;

Label1.Text = "Hello " + tempStr;


The output of splStr=null; where is my wrong?? thanks a lot.
Posted
Updated 3-Apr-11 3:07am
v2

1 solution

The most glaring problem I see is that in your call to the DirectoryEntry constructor, the string parameter has an invalid space in it, namely:

C#
"LDAP:// MyDomain"


It should be:

C#
"LDAP://MyDomain"


Second, you may be missing some parameters inside the string. I recommend that you google "C# DirectoryEntry", and go with one of the 546,000 examples that you get back.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900