Finding a user in Active Directory

G

Guest

'***********************************************
Dim MySearcher As New
DirectorySearcher("LDAP://CN=Users,DC=corporate,DC=domainname,DC=com")

' Create a DirectorySearcher object.
Dim resEnt As SearchResult = MySearcher.FindOne()

'Now what? None of the examples are getting what I need

'***********************************************

I am a newbie and am simply trying to figure out how to query Active
Directory to see if a username being created on the Windows Form already
exists.

I feel like I am really close, but just can't get it.

(p.s. Although I am on A Windows Form, I have added the System.Web &
System.DirectoryServices references)

Thank you very much!

Environment:
Visual Studio 2005
Windows 2003 Mixed-Mode
 
G

Guest

Okay, I have gotten to the point where I can display the Active Directory
path, but still can't find how to just display the username.

For Each mySearchResult As SearchResult In mySearchResultColl
MsgBox(mySearchResult.Path.ToString) 'not what I am looking for,
but very cool. This outlines the LDAP Path!!!
Next
 
S

scott.loomis

Nathan,

Here is something that should get you on your way, note that this is
..net 2003 code

'strcon is your domain, strid is what to connect to the
domain as, and strpwd is strid's domain pwd
Dim Root As DirectoryEntry = New DirectoryEntry(strcon,
strID, strPWD)
Dim ds As System.DirectoryServices.DirectorySearcher(root)
Dim searchresult As DirectoryServices.SearchResult

'check to see if username exists in AD
ds = New DirectoryServices.DirectorySearcher

'strUserName is the username you are trying to create
ds.Filter = String.Format("(SAMAccountName={0})",
strUserName)

'this is common name for example it will return something
like cn=Doe,John if something is found
ds.PropertiesToLoad.Add("cn")

'In version 1.1 of the .Net framwork there was a memory
leak with ds.findone, might be fixed in .net 2.0
searchresult = ds.Findall()

If Len(searchresult.Properties.Item("cn")) = 0 Then
'username not found
'probably not the most efficient wat to see if
something is returned.
else
'username found
end if


That should get you on your way. If you need more let me know.
 

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