6 Replies - 601 Views - Last Post: 25 March 2011 - 06:09 AM Rate Topic: -----

#1 rsturner82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-February 11

Access active directly via VB.Net

Posted 25 March 2011 - 03:36 AM

I having a program built in VB.Net and would like to take in the users windows name and then compatre that with a security group in AD, if they are in a certain group then redirect them to the next page otherwise block them.

I have tried many ways using many different snippets of code from different forums, but cannot find acode piece of code that works.

I am quite new to programming and have no idea how to connect to the AD.

Any help along with actual snippets of code that work would eb a massive help.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Access active directly via VB.Net

#2 MrPerq  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 57
  • Joined: 01-March 11

Re: Access active directly via VB.Net

Posted 25 March 2011 - 03:49 AM

Try searching for LDAP.
Its a protocol used to read AD.

I found this tutorial:
http://www.codeproje...sing_VBnet.aspx

I'm sure more exist.

Keep us updated on your progress!
Was This Post Helpful? 0
  • +
  • -

#3 Cyclopses  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 38
  • Joined: 20-January 11

Re: Access active directly via VB.Net

Posted 25 March 2011 - 03:56 AM

Are you having problems getting the computer's name, or comparing it with something inside the Active Directory?

I must ask, does anyone feel this isn't even near being secure?
Was This Post Helpful? 0
  • +
  • -

#4 rsturner82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-February 11

Re: Access active directly via VB.Net

Posted 25 March 2011 - 03:58 AM

Could you explain a few things in the code below to me please?


Public Function GetAllUsers(ByVal ldapServerName As String) As Hashtable

'To retrieve list of all LDAP users

'This function returns HashTable
_ldapServerName = ldapServerName

Dim sServerName As String = "mail"

Dim oRoot As DirectoryEntry = New DirectoryEntry("LDAP://" & ldapServerName & _
"/ou=People,dc=mydomainname,dc=com")

Dim oSearcher As DirectorySearcher = New DirectorySearcher(oRoot)
Dim oResults As SearchResultCollection
Dim oResult As SearchResult
Dim RetArray As New Hashtable()

Try

oSearcher.PropertiesToLoad.Add("uid")
oSearcher.PropertiesToLoad.Add("givenname")
oSearcher.PropertiesToLoad.Add("cn")
oResults = oSearcher.FindAll

For Each oResult In oResults

If Not oResult.GetDirectoryEntry().Properties("cn").Value = "" Then
RetArray.Add( oResult.GetDirectoryEntry().Properties("uid").Value, _
oResult.GetDirectoryEntry().Properties("cn").Value)
End If

Next

Catch e As Exception

'MsgBox("Error is " & e.Message)
Return RetArray

End Try

Return RetArray

End Function



ldapServerName - this is the server name yes?

"/ou=People,dc=mydomainname,dc=com" - what do i need to insert into each of these?


oSearcher.PropertiesToLoad.Add("uid")
oSearcher.PropertiesToLoad.Add("givenname")
oSearcher.PropertiesToLoad.Add("cn") - do i need to insert anything into the values here in quotes?
Was This Post Helpful? 0
  • +
  • -

#5 MrPerq  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 57
  • Joined: 01-March 11

Re: Access active directly via VB.Net

Posted 25 March 2011 - 04:58 AM

Cyclopses,
I'm sure there must be a way to authenticate against LDAP.
I'm not really sure how though. There must be some snippet out there
that clarifies this.
(Will you be able to query the LDAP from outside the domain?)

Rsturner82,

The sample code below retrieves all users and a few details of these users.
While the sample wont provide the results you wish, it might be helpfull
to understand how to get something from the AD.
Once you understand that, you'll easyly be able to adapt the code
to search for more specific details.
You'll have to learn LDAP queries though.
http://technet.micro...XCHG.65%29.aspx

The parameters to connect to depend on AD & the folder you wish to query for.
That could be for example:
Users/DirectoryName@MyDomain.Com -> LDAP://OU=Users,OU=DirectoryName,DC=MyCompany,DC=com

I simplified it a bit for you.

Dim ConstPCUsersDefaultContainer As String = "LDAP://OU=Users, OU=DirectoryName, DC=MyCompany, DC=com"
Dim UserRoot As New DirectoryEntry(ConstPCUsersDefaultContainer)
Dim Searcher As New DirectorySearcher(UserRoot, "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" & LoginName & "))")
Dim Result As SearchResult = Searcher.FindOne()
Dim Entry As DirectoryEntry = Result.GetDirectoryEntry
Return Entry.Properties("telephonenumber")(0)


This post has been edited by MrPerq: 25 March 2011 - 05:01 AM

Was This Post Helpful? 0
  • +
  • -

#6 rsturner82  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-February 11

Re: Access active directly via VB.Net

Posted 25 March 2011 - 05:42 AM

View PostMrPerq, on 25 March 2011 - 04:58 AM, said:

Cyclopses,
I'm sure there must be a way to authenticate against LDAP.
I'm not really sure how though. There must be some snippet out there
that clarifies this.
(Will you be able to query the LDAP from outside the domain?)

Rsturner82,

The sample code below retrieves all users and a few details of these users.
While the sample wont provide the results you wish, it might be helpfull
to understand how to get something from the AD.
Once you understand that, you'll easyly be able to adapt the code
to search for more specific details.
You'll have to learn LDAP queries though.
http://technet.micro...XCHG.65%29.aspx

The parameters to connect to depend on AD & the folder you wish to query for.
That could be for example:
Users/DirectoryName@MyDomain.Com -> LDAP://OU=Users,OU=DirectoryName,DC=MyCompany,DC=com

I simplified it a bit for you.

Dim ConstPCUsersDefaultContainer As String = "LDAP://OU=Users, OU=DirectoryName, DC=MyCompany, DC=com"
Dim UserRoot As New DirectoryEntry(ConstPCUsersDefaultContainer)
Dim Searcher As New DirectorySearcher(UserRoot, "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" & LoginName & "))")
Dim Result As SearchResult = Searcher.FindOne()
Dim Entry As DirectoryEntry = Result.GetDirectoryEntry
Return Entry.Properties("telephonenumber")(0)




Thank you for your help. I cannot get it too work, but im not sure that im putting correct variables into LDAP://OU=Users,OU=DirectoryName,DC=MyCompany,DC=com

the two DC`s are self explanitory, but what values should go in the OU parts? Directory name, is this the server name?

Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#7 MrPerq  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 57
  • Joined: 01-March 11

Re: Access active directly via VB.Net

Posted 25 March 2011 - 06:09 AM

No, the organizatorial unit within AD where the administrator places its users.
(In a hierarchical map structure, simular to the filesystem.)
How much do you know about Active Directory? Have you browsed a AD before
(outside of code)? If not you might ask your sysadmin to install Exchange and
grant you read-only rights, so you atleast understand the data you are polling.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1