473,327 Members | 1,936 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,327 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 1481
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.