Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / April 2005

Tip: Looking for answers? Try searching our database.

Logged in user's first name using active directory

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mohamed Farouk - 20 Apr 2005 09:49 GMT
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
Paul Clement - 20 Apr 2005 14:11 GMT
¤ 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)
Mohamed Farouk - 20 Apr 2005 22:56 GMT
you ar my hero , thanks a lot

> ¤ i want to get the first name of the logged in user on the current machine,
> ¤ windows application
[quoted text clipped - 33 lines]
> ~~~~
> Microsoft MVP (Visual Basic)
Marc Scheuner [MVP ADSI] - 21 Apr 2005 07:01 GMT
>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
Mohamed Farouk - 21 Apr 2005 11:12 GMT
thanks marc for your help,

> >i want to get the first name of the logged in user on the current machine,
> >windows application
[quoted text clipped - 24 lines]
> HTH
> Marc

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.