Question : VB.NET - Active Directory to find organizational unit for a user

Hello experts,

I created a windows form application that create a record to a database.  The application will get the window user's login name as the creator of that record.  However, I also need to know where they're located.  In Active Directory, we have organizational units for various users.  Is there a way I can find out which organizational unit that user is from?

Here's my code to find out the username that is logged onto the machine:

Dim strUser as String = System.Environment.UserName

I need to find out which organizational unit this user is located within.  Any ideas?

Answer : VB.NET - Active Directory to find organizational unit for a user

This should work:

Dim sCn As String = result.GetDirectoryEntry().Properties("distinguishedname").Value.ToUpper

Dim iLength As Integer = InStr(InStrRev(sCn, "OU="), sCn, ",") - (InStrRev(sCn, "OU=") + 3)
lblOU.Text = Mid(sCn, InStrRev(sCn, "OU=") + 3, iLength)

Random Solutions  
 
programming4us programming4us