473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Active Directories

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")(propertyCounte r),
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

Nov 22 '05 #1
2 1485
Hope this one helps:
http://www.wwwcoder.com/main/parenti...8/default.aspx

"Andrew Alger" <An*********@discussions.microsoft.com> wrote in message
news:29**********************************@microsof t.com...
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")(propertyCounte r),
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


Nov 22 '05 #2
>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
Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andrew Alger | last post by:
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....
1
by: Heath | last post by:
I'm dealing with a C# application that monitors changes to the file system, and need to exclude irrelevent directories, temp directories for example. Is there any way to identify such...
1
by: Gary Townsend | last post by:
Server OS: Windows 2000 Server SP4 IIS: 5.0 Component Language: Visual Basic 6.0 Hey folks quick question hopefully be a quick answer too... I built an ActiveX DLL Component to manipulate file...
1
by: Minal | last post by:
Hello, I've a ASP.net Web application running on one machine and I've 2 active directories which are in 2 different domains. So in all 3 domains. I want my application to create a new user in...
0
by: B111Gates | last post by:
OK I know this is a complex question so I will break it up. I know that SSPI is the prefered method of authentication, however if I use the sample provide by MS I cannot authenticate across...
2
by: Alpha | last post by:
I need to retrieve and set information in Active Directory in a new project that I'll be working on. I went to Amazon.com to look for a good book on this subject but found books on this subject...
9
by: Laurent Bugnion | last post by:
Hi, I am wondering what is the best way to find out which ASP.NET sessions are still active. Here is the reason: I have a custom control which can upload files. It saves the files in a folder...
4
by: rn5a | last post by:
I have a ListBox which should list all the files & directories that exist in a particular directory. The problem is I can get the ListBox to list either all the files or all the directories but not...
6
by: =?Utf-8?B?WW9naSBXYXRjaGVy?= | last post by:
Hello, I am using Visual Studio-2003. I created a project to build my library. Since I am using third party libraries as well, I have specified those additional library dependencies in project...
1
by: Charming12 | last post by:
Hi All, I am very new in accessing microsoft active directories, Most of the part i have covered i.e, Creating a user, assigning a password, Creating Groups etc. But my problem is in my active...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.