473,785 Members | 3,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=User s,DC=Test,DC=lo cal) 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=loca l")
Dim myRepDirectoryS earcher As New DirectorySearch er(mySearchRoot )

myRepDirectoryS earcher.Filter = objectName

' Get the first entry of the search.

Dim myRepSearchResu lt As SearchResult = myRepDirectoryS earcher.FindOne ()

MsgBox(myRepDir ectorySearcher. Filter)

If Not (myRepSearchRes ult Is Nothing) Then

' Get the 'DirectoryEntry ' that corresponds to 'myRepSearchRes ult'.

Dim myDirectoryEntr y As DirectoryEntry =
myRepSearchResu lt.GetDirectory Entry()

MsgBox(myDirect oryEntry.Name)

Dim myRepSearchResu ltPath As String = myRepSearchResu lt.Path

MsgBox(myRepSea rchResultPath)

End If

End Sub
Nov 20 '05 #1
3 3007
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********@inf atech.com> wrote in message
news:cb******** **@sparta.btint ernet.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=User s,DC=Test,DC=lo cal) 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=loca l")
Dim myRepDirectoryS earcher As New DirectorySearch er(mySearchRoot )

myRepDirectoryS earcher.Filter = objectName

' Get the first entry of the search.

Dim myRepSearchResu lt As SearchResult = myRepDirectoryS earcher.FindOne ()

MsgBox(myRepDir ectorySearcher. Filter)

If Not (myRepSearchRes ult Is Nothing) Then

' Get the 'DirectoryEntry ' that corresponds to 'myRepSearchRes ult'.

Dim myDirectoryEntr y As DirectoryEntry =
myRepSearchResu lt.GetDirectory Entry()

MsgBox(myDirect oryEntry.Name)

Dim myRepSearchResu ltPath As String = myRepSearchResu lt.Path

MsgBox(myRepSea rchResultPath)

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.Propertie s("defaultNamin gContext").Valu e
Dim Results As SearchResultCol lection, 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 DirectorySearch er(Entry)
With Searcher
.SearchScope = SearchScope.Sub tree
.Filter = "(&(objectClass =person)(sAMAcc ountName=" & UserName &
"))"
'Only works when threaded. Keep this in mind.
'Default size returned is 1000 (server set)
.PageSize = 100
.PropertiesToLo ad.AddRange(New String() {"sAMAccountNam e",
"displayNam e"})
Result = .FindOne
End With
If Not Result Is Nothing Then
'Watch for multivalued properties
If TypeOf Result.GetDirec toryEntry.Prope rties("displayN ame") Is
System.Array Then
MessageBox.Show (Result.GetDire ctoryEntry.Prop erties("display Name")(0).Value )
Else
MessageBox.Show (Result.GetDire ctoryEntry.Prop erties("display Name").Value)
End If
End If
With Searcher
'You can use wild cards, Look up the RFC on LDAP for more info
.Filter = "(&(objectClass =person)(sAMAcc ountName=" & UserName &
"*))"
Results = Searcher.FindAl l
End With
For Each Result In Results
'Same as before
If TypeOf Result.GetDirec toryEntry.Prope rties("displayN ame") Is
System.Array Then
Console.WriteLi ne(Result.GetDi rectoryEntry.Pr operties("displ ayName")(0).Val ue)
Else
Console.WriteLi ne(Result.GetDi rectoryEntry.Pr operties("displ ayName").Value)
End If
Next
End Sub

"Phil Kelly" <ph********@inf atech.com> wrote in message
news:cb******** **@sparta.btint ernet.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=User s,DC=Test,DC=lo cal) 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=loca l")
Dim myRepDirectoryS earcher As New DirectorySearch er(mySearchRoot )

myRepDirectoryS earcher.Filter = objectName

' Get the first entry of the search.

Dim myRepSearchResu lt As SearchResult = myRepDirectoryS earcher.FindOne ()

MsgBox(myRepDir ectorySearcher. Filter)

If Not (myRepSearchRes ult Is Nothing) Then

' Get the 'DirectoryEntry ' that corresponds to 'myRepSearchRes ult'.

Dim myDirectoryEntr y As DirectoryEntry =
myRepSearchResu lt.GetDirectory Entry()

MsgBox(myDirect oryEntry.Name)

Dim myRepSearchResu ltPath As String = myRepSearchResu lt.Path

MsgBox(myRepSea rchResultPath)

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.supe rnews.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********@inf atech.com> wrote in message
news:cb******** **@sparta.btint ernet.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=User s,DC=Test,DC=lo cal) 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=loca l")
Dim myRepDirectoryS earcher As New DirectorySearch er(mySearchRoot )

myRepDirectoryS earcher.Filter = objectName

' Get the first entry of the search.

Dim myRepSearchResu lt As SearchResult = myRepDirectoryS earcher.FindOne ()

MsgBox(myRepDir ectorySearcher. Filter)

If Not (myRepSearchRes ult Is Nothing) Then

' Get the 'DirectoryEntry ' that corresponds to 'myRepSearchRes ult'.

Dim myDirectoryEntr y As DirectoryEntry =
myRepSearchResu lt.GetDirectory Entry()

MsgBox(myDirect oryEntry.Name)

Dim myRepSearchResu ltPath As String = myRepSearchResu lt.Path

MsgBox(myRepSea rchResultPath)

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
3948
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 list and group list separatedly.there is my code that downloaded from the internet : public class LdapAuthentication { private string _path;
10
5454
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 machine then it works again. here is one of the method where am calling the AD public bool UserExist(string UserName) {
0
1848
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 on my test system which is a win2K Server running as a domain controller and web server. The second system is a win2K workstation connected to the live win2K domain controller, This computer was able to run the code in the past and now all I keep...
4
3804
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 information using the syntax "LDAP://"?
1
3895
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 = New DirectoryEntry("LDAP://domain") Dim mySearcher As New DirectorySearcher(enTry) Dim resEnt As SearchResult mySearcher.Filter = ("(objectClass=*)") mySearcher.SearchScope = SearchScope.Subtree
10
4065
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 Pop up windows Authentication box so that user will give his userId, Password & domain name for authenticaion. After that I want to take these three info of user and make a search in Active Directory.
2
4698
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 because all we do is verify the user on the login form so we can redirect them to the appropriate page based on their group. The code to authenticate is: Public Function IsAuthenticated(ByVal domain As String, ByVal username As String, ByVal...
3
2054
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 will repeatedly occur for a period of time then start working fine again. ASP.NET application uses windows authentication. I am attaching the code for reference: -----------------------------
7
14244
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 local machine but the FindOne() method fails when I deploy it to the server. I'm at a loss and any help would be appreciated. This is the code that works on my local machine but not on the server. DirectorySearcher search = new...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9947
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.