Active Directories

G

Guest

I am having a hard time trying to communicate to the Active Directories to
retrieve the following:
User Name
Email Address
Phone
Groups User is assigned to

The last one is the most important. I am trying to develop this in VB.NET.
Code Follows. Please give code examples if possible.

Thanks,

Imports System.DirectoryServices

Dim m_obDirEntry As New DirectoryEntry("LDAP://[DOMAIN]/", )
Dim search As DirectorySearcher = New DirectorySearcher(m_obDirEntry)
search.Filter = "(cn=" & [username] & ")"
search.PropertiesToLoad.Add("memberOf")
Dim groupNames As StringBuilder = New StringBuilder

Dim result As SearchResult = search.FindOne()
Dim propertyCount As Integer = result.Properties("memberOf").Count

Dim dn As String
Dim equalsIndex, commaIndex

Dim propertyCounter As Integer

For propertyCounter = 0 To propertyCount - 1
dn = CType(result.Properties("memberOf")(propertyCounter),
String)

equalsIndex = dn.IndexOf("=", 1)
commaIndex = dn.IndexOf(",", 1)
If (equalsIndex = -1) Then
End If

groupNames.Append(dn.Substring((equalsIndex + 1),
(commaIndex - equalsIndex) - 1))
groupNames.Append("|")
Next



TextBox1.Text = groupNames.ToString
 
S

Shiva

Hope this one helps:
http://www.wwwcoder.com/main/parentid/260/site/2208/68/default.aspx

I am having a hard time trying to communicate to the Active Directories to
retrieve the following:
User Name
Email Address
Phone
Groups User is assigned to

The last one is the most important. I am trying to develop this in VB.NET.
Code Follows. Please give code examples if possible.

Thanks,

Imports System.DirectoryServices

Dim m_obDirEntry As New DirectoryEntry("LDAP://[DOMAIN]/", )
Dim search As DirectorySearcher = New
DirectorySearcher(m_obDirEntry)
search.Filter = "(cn=" & [username] & ")"
search.PropertiesToLoad.Add("memberOf")
Dim groupNames As StringBuilder = New StringBuilder

Dim result As SearchResult = search.FindOne()
Dim propertyCount As Integer =
result.Properties("memberOf").Count

Dim dn As String
Dim equalsIndex, commaIndex

Dim propertyCounter As Integer

For propertyCounter = 0 To propertyCount - 1
dn = CType(result.Properties("memberOf")(propertyCounter),
String)

equalsIndex = dn.IndexOf("=", 1)
commaIndex = dn.IndexOf(",", 1)
If (equalsIndex = -1) Then
End If

groupNames.Append(dn.Substring((equalsIndex + 1),
(commaIndex - equalsIndex) - 1))
groupNames.Append("|")
Next



TextBox1.Text = groupNames.ToString
 
M

Marc Scheuner [MVP ADSI]

I am having a hard time trying to communicate to the Active Directories to
retrieve the following:
User Name
Email Address
Phone
Groups User is assigned to

Okay, first off - you need the LDAP attribute names for those pieces
of information. You can find a great Excel sheet with all that stuff
at http://www.rlmueller.net - check the "Links and References"
section.

Also, you might want to have a look at my BeaverTail ADSI Browser -
shows your AD objects in their hiearchy, with all their attributes
that contain values - quite helpful to get started and to learn about
AD objects!

Get it for free from here:
http://adsi.mvps.org/adsi/CSharp/beavertail.html

Which user name exactly are you interested in? There's tons.... the
"display name" ? The actual user object's name (the CN?) ? The
pre-Win2000 login name (a.k.a the SAMAccountName ?). Take your pick!

Email Address is stored in "mail", Phone is in "telephoneNumber", and
group membership - tricky one - there's the "memberOf" attribute,
which lists most of the groups a user is a member of (duh!) - but
*NOT* the so called primary group (usually "domain users"), and *NOT*
any nested group membership (John Doe is member of Group B, because
he's member of Group A which in turn is member of Group B).

If you know the user's LDAP path - which is something like:

LDAP://cn=John Doe,ou=Research,ou=Headquarters,dc=YourCompany,dc=com

then you can bind to it directly.

If you only know your company's domain (e.g.
LDAP://dc=yourCompany,dc=com), then you need to search for the user by
using a DirectorySearcher.
Dim m_obDirEntry As New DirectoryEntry("LDAP://[DOMAIN]/", )

Dim m_obDirEntry As New
DirectoryEntry("LDAP://dc=YourCompany,dc=com/")
Dim search As DirectorySearcher = New DirectorySearcher(m_obDirEntry)
search.Filter = "(cn=" & [username] & ")"
search.PropertiesToLoad.Add("memberOf")

Also add:

search.PropertiesToLoad.Add("mail")
search.PropertiesToLoad.Add("telephoneNumber")
Dim result As SearchResult = search.FindOne()
Dim propertyCount As Integer = result.Properties("memberOf").Count

Gotta be careful here !! First of all, if the .FindOne() call fails,
your "result" will be NULL (or Nothing, in VB parlance) - you gotta
check for that before accessing properties of that object!

Also, if your user doesn't belong to anything but the primary group,
the result.Properties["memberOf"] will be Nothing, so you gotta check
for that, too, before accessing its .Count property!

HTH
Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 

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