473,395 Members | 1,539 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.

Small vb.NET/Active Directory FindOne Problem

Hi!

I hope someone can help me here because I'm tearing my hair out (what little
there is of it!) trying to figure out what's going on with the code below.

I'm passing an Active Directory CN of a user object (like
CN=Phil,OU=Users,DC=Test,DC=local) to the doRep() function, then have the
function search for the user in AD ('FindOne')

Then, I'm trying to get the code to msgbox the directory entry name.... but
we never seem to get that far; nothing turns up in a msgbox

Now, I'm not that au fait with vb .NET but I'm finding the challenge to be
superb. I really hope someone will show me the error of my ways...!

Thanks

Phil
Public Sub doRep(ByVal objectName As String)

' Create a 'DirectoryEntry' object to search.

Dim mySearchRoot As New DirectoryEntry("LDAP://DC=test,DC=local")
Dim myRepDirectorySearcher As New DirectorySearcher(mySearchRoot)

myRepDirectorySearcher.Filter = objectName

' Get the first entry of the search.

Dim myRepSearchResult As SearchResult = myRepDirectorySearcher.FindOne()

MsgBox(myRepDirectorySearcher.Filter)

If Not (myRepSearchResult Is Nothing) Then

' Get the 'DirectoryEntry' that corresponds to 'myRepSearchResult'.

Dim myDirectoryEntry As DirectoryEntry =
myRepSearchResult.GetDirectoryEntry()

MsgBox(myDirectoryEntry.Name)

Dim myRepSearchResultPath As String = myRepSearchResult.Path

MsgBox(myRepSearchResultPath)

End If

End Sub
Nov 20 '05 #1
3 2983
Phil
If you already know the distinguished name then you don't have to search for
it.

Dim MyEntry As New DirectoryEntry("LDAP://" & objectName)

Give me a few min and I will give you an example on searching.

Jared

"Phil Kelly" <ph********@infatech.com> wrote in message
news:cb**********@sparta.btinternet.com...
Hi!

I hope someone can help me here because I'm tearing my hair out (what
little
there is of it!) trying to figure out what's going on with the code below.

I'm passing an Active Directory CN of a user object (like
CN=Phil,OU=Users,DC=Test,DC=local) to the doRep() function, then have the
function search for the user in AD ('FindOne')

Then, I'm trying to get the code to msgbox the directory entry name....
but
we never seem to get that far; nothing turns up in a msgbox

Now, I'm not that au fait with vb .NET but I'm finding the challenge to be
superb. I really hope someone will show me the error of my ways...!

Thanks

Phil
Public Sub doRep(ByVal objectName As String)

' Create a 'DirectoryEntry' object to search.

Dim mySearchRoot As New DirectoryEntry("LDAP://DC=test,DC=local")
Dim myRepDirectorySearcher As New DirectorySearcher(mySearchRoot)

myRepDirectorySearcher.Filter = objectName

' Get the first entry of the search.

Dim myRepSearchResult As SearchResult = myRepDirectorySearcher.FindOne()

MsgBox(myRepDirectorySearcher.Filter)

If Not (myRepSearchResult Is Nothing) Then

' Get the 'DirectoryEntry' that corresponds to 'myRepSearchResult'.

Dim myDirectoryEntry As DirectoryEntry =
myRepSearchResult.GetDirectoryEntry()

MsgBox(myDirectoryEntry.Name)

Dim myRepSearchResultPath As String = myRepSearchResult.Path

MsgBox(myRepSearchResultPath)

End If

End Sub

Nov 20 '05 #2
Phill,
I hope this helps, I haven't tested this exact code, it should work
though. I have a simple control inherited from treeview that lets you get
the search root. Just drop it on your form, call one method and it will let
you select and OU or container. If you are interested let me know.

Jared

Sub doRep(ByVal UserName As String) 'User name - NOT display name

Dim Entry As New DirectoryEntry("LDAP://RootDSE")
Dim DomainPath As String = "LDAP://" &
Entry.Properties("defaultNamingContext").Value
Dim Results As SearchResultCollection, Result As SearchResult
'Domain Naming Context
'DC=MyDomain,DC=com
Entry = New DirectoryEntry(DomainPath)
'I've tried to set the path directly but it is empty, so I started
using a path variable
Dim Searcher As New DirectorySearcher(Entry)
With Searcher
.SearchScope = SearchScope.Subtree
.Filter = "(&(objectClass=person)(sAMAccountName=" & UserName &
"))"
'Only works when threaded. Keep this in mind.
'Default size returned is 1000 (server set)
.PageSize = 100
.PropertiesToLoad.AddRange(New String() {"sAMAccountName",
"displayName"})
Result = .FindOne
End With
If Not Result Is Nothing Then
'Watch for multivalued properties
If TypeOf Result.GetDirectoryEntry.Properties("displayName") Is
System.Array Then
MessageBox.Show(Result.GetDirectoryEntry.Propertie s("displayName")(0).Value)
Else
MessageBox.Show(Result.GetDirectoryEntry.Propertie s("displayName").Value)
End If
End If
With Searcher
'You can use wild cards, Look up the RFC on LDAP for more info
.Filter = "(&(objectClass=person)(sAMAccountName=" & UserName &
"*))"
Results = Searcher.FindAll
End With
For Each Result In Results
'Same as before
If TypeOf Result.GetDirectoryEntry.Properties("displayName") Is
System.Array Then
Console.WriteLine(Result.GetDirectoryEntry.Propert ies("displayName")(0).Value)
Else
Console.WriteLine(Result.GetDirectoryEntry.Propert ies("displayName").Value)
End If
Next
End Sub

"Phil Kelly" <ph********@infatech.com> wrote in message
news:cb**********@sparta.btinternet.com...
Hi!

I hope someone can help me here because I'm tearing my hair out (what
little
there is of it!) trying to figure out what's going on with the code below.

I'm passing an Active Directory CN of a user object (like
CN=Phil,OU=Users,DC=Test,DC=local) to the doRep() function, then have the
function search for the user in AD ('FindOne')

Then, I'm trying to get the code to msgbox the directory entry name....
but
we never seem to get that far; nothing turns up in a msgbox

Now, I'm not that au fait with vb .NET but I'm finding the challenge to be
superb. I really hope someone will show me the error of my ways...!

Thanks

Phil
Public Sub doRep(ByVal objectName As String)

' Create a 'DirectoryEntry' object to search.

Dim mySearchRoot As New DirectoryEntry("LDAP://DC=test,DC=local")
Dim myRepDirectorySearcher As New DirectorySearcher(mySearchRoot)

myRepDirectorySearcher.Filter = objectName

' Get the first entry of the search.

Dim myRepSearchResult As SearchResult = myRepDirectorySearcher.FindOne()

MsgBox(myRepDirectorySearcher.Filter)

If Not (myRepSearchResult Is Nothing) Then

' Get the 'DirectoryEntry' that corresponds to 'myRepSearchResult'.

Dim myDirectoryEntry As DirectoryEntry =
myRepSearchResult.GetDirectoryEntry()

MsgBox(myDirectoryEntry.Name)

Dim myRepSearchResultPath As String = myRepSearchResult.Path

MsgBox(myRepSearchResultPath)

End If

End Sub

Nov 20 '05 #3
Thanks Jared!

That's good to know (works fine too....)

I owe you one!

Phil
"Jared" <VB***********@email.com> wrote in message
news:10*************@corp.supernews.com...
Phil
If you already know the distinguished name then you don't have to search for it.

Dim MyEntry As New DirectoryEntry("LDAP://" & objectName)

Give me a few min and I will give you an example on searching.

Jared

"Phil Kelly" <ph********@infatech.com> wrote in message
news:cb**********@sparta.btinternet.com...
Hi!

I hope someone can help me here because I'm tearing my hair out (what
little
there is of it!) trying to figure out what's going on with the code below.
I'm passing an Active Directory CN of a user object (like
CN=Phil,OU=Users,DC=Test,DC=local) to the doRep() function, then have the function search for the user in AD ('FindOne')

Then, I'm trying to get the code to msgbox the directory entry name....
but
we never seem to get that far; nothing turns up in a msgbox

Now, I'm not that au fait with vb .NET but I'm finding the challenge to be superb. I really hope someone will show me the error of my ways...!

Thanks

Phil
Public Sub doRep(ByVal objectName As String)

' Create a 'DirectoryEntry' object to search.

Dim mySearchRoot As New DirectoryEntry("LDAP://DC=test,DC=local")
Dim myRepDirectorySearcher As New DirectorySearcher(mySearchRoot)

myRepDirectorySearcher.Filter = objectName

' Get the first entry of the search.

Dim myRepSearchResult As SearchResult = myRepDirectorySearcher.FindOne()

MsgBox(myRepDirectorySearcher.Filter)

If Not (myRepSearchResult Is Nothing) Then

' Get the 'DirectoryEntry' that corresponds to 'myRepSearchResult'.

Dim myDirectoryEntry As DirectoryEntry =
myRepSearchResult.GetDirectoryEntry()

MsgBox(myDirectoryEntry.Name)

Dim myRepSearchResultPath As String = myRepSearchResult.Path

MsgBox(myRepSearchResultPath)

End If

End Sub


Nov 20 '05 #4

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

Similar topics

1
by: Toufani | last post by:
Hi everybody, I want to retrieve information about objects in active directory windows 2000 and their properties. I got some codes that don't work absolutely. for example I can't retrieve users...
10
by: huzz | last post by:
I have web application that quaries the Active Directory to get user details.. everything works fine but someday I'll get System.Runtime.InteropServices.COMExection and if I restart the client...
0
by: Kenneth Keeley | last post by:
Hi, I have been working on a Login page that uses ADSI to authenicate the users. I had this all working on my test system and on a second system connected to the live domain. Now it will only work...
4
by: ptran123 | last post by:
I want to do an AD lookup of the current logged on user. From ASP.NET I can get the identity name of the user in the form of "<domain>\<user>". How do I perform an AD search based on that...
1
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
10
by: Hriday | last post by:
Hi there, Please help me..It is urgent This is Hriday, working on windows authentication with Active Directory... My requirment is when a user sends a request to my web Applicatoin I want to...
2
by: P Webster | last post by:
We recently moved a web site that validated user credentials in Active Directory from IIS 5.1 to IIS 6, and the validation code no longer works. The web.config file is set to Windows authentication...
3
by: Susan | last post by:
Hello all, My ASP.NET application seems to have intermittent problems when connecting to Active Directory server. Most of the time the Active Directory app works fine then suddenly fails and...
7
by: =?Utf-8?B?TWlrZQ==?= | last post by:
I'm trying to use the DirectorySearcher.FindOne() method to get the display name of the current user. The code runs on a server behind a web service. Everything works fine when I run it on my...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.