Logged in user's first name using active directory

G

Guest

i want to get the first name of the logged in user on the current machine,
windows application

i used the following code but i get System.NullReferenceException when it
reaches the Console.Writeline line

string myADSPath = "LDAP://myadelaide.com/CN=Users,DC=myadelaide,DC=com" ;
DirectoryEntry myDirectoryEntry = new DirectoryEntry(myADSPath);
DirectoryEntry mySearchRoot = new DirectoryEntry(myADSPath);
DirectorySearcher myDirectorySearcher =new DirectorySearcher(mySearchRoot);
myDirectorySearcher.Filter="(objectClass=user)";
SearchResult mySearchResult = myDirectorySearcher.FindOne();

myDirectoryEntry.Name
if ( mySearchResult != null )
{
myDirectoryEntry =mySearchResult.GetDirectoryEntry();

Console.WriteLine(myDirectoryEntry.Properties["First
name"].Value.ToString() );


}

though i can get the DirectoryEntry.Name but any other-
DirectoryEntry.Properties["Property name"]- dosen't work though the property
count is 35 when i put DirectoryEntry.Properties into quick watch.

any help please
 
P

Paul Clement

¤ i want to get the first name of the logged in user on the current machine,
¤ windows application
¤
¤ i used the following code but i get System.NullReferenceException when it
¤ reaches the Console.Writeline line
¤
¤ string myADSPath = "LDAP://myadelaide.com/CN=Users,DC=myadelaide,DC=com" ;
¤ DirectoryEntry myDirectoryEntry = new DirectoryEntry(myADSPath);
¤ DirectoryEntry mySearchRoot = new DirectoryEntry(myADSPath);
¤ DirectorySearcher myDirectorySearcher =new DirectorySearcher(mySearchRoot);
¤ myDirectorySearcher.Filter="(objectClass=user)";
¤ SearchResult mySearchResult = myDirectorySearcher.FindOne();
¤
¤ myDirectoryEntry.Name
¤ if ( mySearchResult != null )
¤ {
¤ myDirectoryEntry =mySearchResult.GetDirectoryEntry();
¤
¤ Console.WriteLine(myDirectoryEntry.Properties["First
¤ name"].Value.ToString() );
¤
¤
¤ }
¤
¤ though i can get the DirectoryEntry.Name but any other-
¤ DirectoryEntry.Properties["Property name"]- dosen't work though the property
¤ count is 35 when i put DirectoryEntry.Properties into quick watch.
¤
¤ any help please

I think you're looking for givenName:

..GetDirectoryEntry().Properties.Item("givenName").Value


Paul
~~~~
Microsoft MVP (Visual Basic)
 
M

Marc Scheuner [MVP ADSI]

i want to get the first name of the logged in user on the current machine,
windows application
any help please

That seems a bit complicated - you could do it a lot easier:

1) Add a reference to the COM "Active DS Type Library" to your project
2) Add a "using ActiveDs; " line to your .cs file

3) Get the current user from the IADsADSystemInfo interface:

IADsADSystemInfo oSysInfo = new ADSystemInfoClass();
string sCurrentUserDN = oSysInfo.UserName;

4) Bind to that user and get it's first name ("givenName")

DirectoryEntry deCurrentUser = new DirectoryEntry("LDAP://" +
sCurrentUserDN);

if(deCurrentUser != null &&
deCurrentUser.Properties.Contains("givenName"))
{
string sFirstName =
deCurrentUser.Properties["givenName"].Value.ToString();
}

HTH
Marc
 
G

Guest

thanks marc for your help,

Marc Scheuner said:
i want to get the first name of the logged in user on the current machine,
windows application
any help please

That seems a bit complicated - you could do it a lot easier:

1) Add a reference to the COM "Active DS Type Library" to your project
2) Add a "using ActiveDs; " line to your .cs file

3) Get the current user from the IADsADSystemInfo interface:

IADsADSystemInfo oSysInfo = new ADSystemInfoClass();
string sCurrentUserDN = oSysInfo.UserName;

4) Bind to that user and get it's first name ("givenName")

DirectoryEntry deCurrentUser = new DirectoryEntry("LDAP://" +
sCurrentUserDN);

if(deCurrentUser != null &&
deCurrentUser.Properties.Contains("givenName"))
{
string sFirstName =
deCurrentUser.Properties["givenName"].Value.ToString();
}

HTH
Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top