DirectoryServices Problem

  • Thread starter Keith Jakobs, MCP
  • Start date
K

Keith Jakobs, MCP

Hi All....

I'm having a HECK of a time connecting to Active Directory using VB in
Visual Studio.NET 2003. Can anyone PLEASE help me?

All I am trying to do is list the current members of our Active Directory on
a web page. If I can connect to the AD interfaces, then I think I can
handle it from there.

I do have a VS.NET C# project successfully working and enumerating names,
but can NOT for the life of me get VB to work with AD.

I originally tried to do it outside of VS.NET, but ran into problems trying
to reference the System.DirectoryServices namespace unless I compiled the
project inside VS.NET. I did make an earlier post to this group about this
issue, and though the recommendations I received were insightful, they did
not work (neither precompiling, nor including a direct reference to the .dll
in the Page directive worked). I was finally able to import the name space
by copying the dll to the application's /bin directory. But why would I
want to make a copy of it when it already exists on the system? So what
happened to being able to build .NET applications with just a text editor???

So I decided to try a walkthrough of MS Knowledge Base article # 326340
(http://support.microsoft.com/default.aspx?scid=kb;en-us;326340&Product=aspn
et). I followed it to the letter, and when I try to build the project in
VS.NET, I get compilation errors in the .vb file I cut and pasted directly
from the article!!!! The three errors are all in the LdapAuthentication.vb
file:

E:\inetpub\AuthTest\LdapAuthentication.vb(19): Array bounds cannot appear in
type specifiers.
E:\inetpub\AuthTest\LdapAuthentication.vb(23): Name 'entry' is not declared.
E:\inetpub\AuthTest\LdapAuthentication.vb(24): Name 'entry' is not declared.

So.... if anyone can PLEASE HELP me get going here (I'm almost hoping one of
you can confirm my install is pooched, otherwise I have no idea why it does
not like the instructions as entered according to MS)

Again, all I need to do is connect to AD from a Web Page and list all the
users (Intranet site only).

Thanks in advance!

Keith C. Jakobs, MCP
 
K

Keith Jakobs, MCP

I guess a little more details on the errors would be useful....
E:\inetpub\AuthTest\LdapAuthentication.vb(19): Array bounds cannot appear
in
type specifiers. (refers to '_path' in):
Dim entry As DirectoryEntry(_path, domainAndUsername, pwd)
E:\inetpub\AuthTest\LdapAuthentication.vb(23): Name 'entry' is not
declared. refers to:
Dim obj As Object = entry.NativeObject

and
E:\inetpub\AuthTest\LdapAuthentication.vb(24): Name 'entry' is not
declared. refers to:
Dim search As DirectorySearcher = New DirectorySearcher(entry)

Which completely confuses me because 'entry' is deinfed in the first error
line, and a statement taken from MS instructions!!!!

..NET is soooo damn confusing. Please Help!


Thanks again!

Keith C. Jakobs, MCP
 
G

Greg Burns

I do not have System.DirectoryServices.dll copied to my bin folder (copy
local=false), but I do have it referenced in VS.

Here is the AD code I am using in a web app, with no problems. Might want to
try this little function out and see if you can get it to work. Make sure
your virtual directory has anonymous access turned off.

Greg

Imports System.DirectoryServices
Public Class MyFunctions
Public Shared Function GetFullName(ByVal UserName As String) As String
Const DomainName As String = "mydomain.com"

Dim oDirectory As New DirectoryEntry("LDAP://" & DomainName)
Dim mySearcher As New DirectorySearcher(oDirectory)
Dim oResult As SearchResult
Dim sResult As String
mySearcher.SearchScope = SearchScope.Subtree
mySearcher.ReferralChasing = ReferralChasingOption.All
mySearcher.Filter = "(&(objectClass=user)(sAMAccountName=" &
UserName & "))"

Try
oResult = mySearcher.FindOne
If Not oResult Is Nothing Then
sResult =
oResult.GetDirectoryEntry.Properties("DisplayName").Value.ToString()
End If
Catch ex As Exception
Throw ex
End Try

oResult = Nothing
mySearcher.Dispose()
oDirectory.Dispose()

Return sResult
End Function
End Class
 
K

Keith Jakobs, MCP

Hi Greg,

Thanks so much for your response and sample code. Though I do appreciate
it, the .NET architecture has me completely confused. I have been
programming since I wrote Machine Language for 8088 and Z80 CPU's, and have
been published in Pascal.... but there are so many layers to .NET, I have no
idea how everything interfaces....

Though your sample code is not completely alien to me... I have no idea ....

(a) how to call it from a web page! How do I attach it to a form, or tell a
form to inherit the function (whichever is the correct terminology)???? I
could guess and play around, but right now, I'm just trying to get one
feature working, and will have no idea which part is not correct (the AD
code, or the connection to it???). I dont even know if I my files are
interconnected correctly at this point. Can I add it to an existing vb
file? Can I tell it to just compile the one form and associated code, so I
can ignore any other errors introduced by modifying global.asax or
web.config, or do I have to create a whole new project just to test this
little snippet. And then how do I connect the code to a form if I dont have
a namespace for it???

(b) what do I pass to it? I see the ByVal UserName parameter, and
understand ByVal parameter references... but where am I getting a UserName
to pass to it, or do I even need it?

BTW, since switching over to trying to make this work just in VS.NET, I have
included the reference to System.DirectoryServices. It no longer complains
about being able to import the namespace, but it still seems to have no idea
about what a DirectoryEntry is!

It's all so very confusing. I'm sorry, but I need a lot more direction on
how to test this code, and though I dont expect you to spend time doing so,
I am hoping someone out there knows a great reference or link that can help
solve my confusion regarding VS.NET project management and specific file
integration.

I do appreciate your help, but wish I knew what I am missing that makes .NET
seem so bewildering to me. I am not a newbie, I have been coding for a
fifth of a century and it is very frustrating that I am having so much of a
problem understanding how all these files work together in the .NET
architecture. And for that matter why I can not connect to the Directory
Services using VB!

I think I liked ADSI and ASP much better.... this is ridiculous! I never
had a problem interfacing with ADSI even using VBA, and have programmed some
very complicated routines using that model!

If anyone can point me to some decent comprehensive and TUTORIAL
documentation on how to use System.DirectoryServices in VB, it would be
greatly appreciated. Any books anyone would recommend??? Thanks!!!

Keith C. Jakobs, MCP
 
G

Greg Burns

You sound like me back when beta 2 was released. :^)

If you code doesn't know what DirectoryEntry is, even after referencing
System.DirectoryServices then you probably have not added the imports
statement at the top of your class file:

Imports System.DirectoryServices

This is just a shortcut, so you don't have to type
System.DirectoryServices.DirectoryEntry

As far as using my code. I would suggest making a new project to play. (I
took a look at that link to MS example, and they were doing some heady
things with your default web.config and stuff that will just confuse the
issue at hand).

Just add a new class file to your project. Right-click your project root,
choose Add New Item, select Class file. Name it MyFunctions.vb. Now just
past in my code.

Then from within your Page_Load method of, say your default.aspx page, do
this:

response.write(MyFunctions.GetFullName("mydomain\burnsg"))

where the username is a valid username on your domain.

Just to be clear, this function doesn't do what you are trying to do. It
just takes a username and looks up the displayname from AD. It is just a
test to see that your can talk to AD.
(a) how to call it from a web page! How do I attach it to a form, or tell a
form to inherit the function (whichever is the correct terminology)???? I

I would use the term "calling a method on a shared class". Another approach
(more complicated by far) would to have the MyFuctions class inherit from
Page. And have all of your .aspx pages inherit from MyFuctions rather than
the Page class. That way you could just call GetFullName directly from
Page_Load without the qualifier. But that is overkill for this exmample.

Let me know if I can help some more.

Greg
 
K

Keith Jakobs, MCP

Hi Greg!

Thanks so much for taking the additional time to clarify some of those
issues and provide more detailed instructions.

I will give those a try and see how successful I am. FYI though, I do have
the 'Imports System.DirectoryServices' in my class file, but it wasnt until
I compiled it within VS.NET that the IIS server knew what to do with it!

Keith

Keith C. Jakobs, MCP
 

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