|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I am very new to the VB.NET Web Forms, and I am creating this forms that will query the Active Directory. I need it
to return information like the user's email address. I know about the AD, but I just don't know how to access it through VB.NET Web Forms. Could someone help please? |
|
#2
|
|||
|
|||
|
I really need some help on this, can someone please give me a life line.
Quote:
|
|
#3
|
|||
|
|||
|
I can't believe this. No one knows how this is done? Please this is killing me.
|
|
#4
|
|||
|
|||
|
get email from AD
[hear is an example for email from AD] hope this helps
PublicFunction GetEmailFromActDir(ByVal strUserName AsString) AsString '################################################# #################### '# gets user email from active dir based on the username, to connect # '# to the active dir the userid as per connection has to be specified# '# builds ldap function query, if there are more then one result for # '# the search - exits the function, if only one then get the address # '# from the properties collection # '################################################# #################### Dim strEmailAddress AsString Try Dim strPath AsString = LDAP://????.com/DC=????,DC=com Dim strUserIds AsString = "????\userid, password" '#################!!!!!!!!!#### Dim objDirEntry AsNew System.DirectoryServices.DirectoryEntry(strPath, "????\userid", "password", DirectoryServices.AuthenticationTypes.Secure) Dim objDirSearcher AsNew System.DirectoryServices.DirectorySearcher(objDirE ntry) Dim objCollSearchResult As System.DirectoryServices.SearchResultCollection Dim objlSearchResult As System.DirectoryServices.SearchResult Dim objCollResultProperty As System.DirectoryServices.ResultPropertyCollection Dim objCollResultPropertyValue As System.DirectoryServices.ResultPropertyValueCollec tion objDirSearcher.Filter = ("(&(objectClass=user)(samaccountname=" & strUserName & "))") objCollSearchResult = objDirSearcher.FindAll() SelectCase objCollSearchResult.Count Case 0 strEmailAddress = "" CaseIs > 1 ExitFunction CaseIs = 1 objlSearchResult = objCollSearchResult.Item(0) objCollResultProperty = objlSearchResult.Properties objCollResultPropertyValue = objCollResultProperty.Item("mail") strEmailAddress = objCollResultPropertyValue.Item(0) EndSelect '################################################# ######### '# forced disposing # '################################################# ######### objDirEntry.Dispose() objDirSearcher.Dispose() objCollSearchResult.Dispose() objlSearchResult = Nothing objCollResultProperty = Nothing objCollResultPropertyValue = Nothing Catch ex As System.Exception Dim strMess AsString strMess = ex.Message strEmailAddress = "" EndTry Return strEmailAddress EndFunction |
|
#5
|
|||
|
|||
|
now where would I place this code so I could use the strEmailAddress on the other forms?
|
|
#6
|
|||
|
|||
|
Also, do I have to input the userid and password? What I want to happen is the user go the url of the web form and when they get to the page that request the email address I want the form to return their email address from active directory. I don't want the userid and password exposed to anyone. Is it possible?
|
|
#7
|
|||
|
|||
|
Yes, it is imperitive that you supply the username for authentication purposes required by LDAP (according to my knowledge), otherwise, how do you indicate to LDAP which users email addresses you want to extract? So it makes only sense.
To be able to use the returned emailaddress string variable in your other forms you must declare a public variable to save the returned value in, which you can subsequently use throughout your Windows Forms Application. Here is an example: Add a new Module to your project and declare a Public variable inside your new module: Module Module1 Public EmailAddress As String 'This variable is now accessible throughout your whole application. EndModule PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Assign the email address returned by the function to the public variable - you can now use this variable throughout 'your application EmailAddress = GetEmailFromActDir("JohnD") EndSub Public Function GetEmailFromActDir(ByVal strUserName AsString) AsString '################################################# #################### '# gets user email from active dir based on the username, to connect # '# to the active dir the userid as per connection has to be specified# '# builds ldap function query, if there are more then one result for # '# the search - exits the function, if only one then get the address # '# from the properties collection # '################################################# #################### Dim strEmailAddress AsString Try Dim strPath AsString = LDAP://????.com/DC=????,DC=com (ldap:///????.com/DC=????,DC=com) Dim strUserIds AsString = "????\userid, password" '#################!!!!!!!!!#### Dim objDirEntry AsNew System.DirectoryServices.DirectoryEntry(strPath, "????\userid", "password", DirectoryServices.AuthenticationTypes.Secure) Dim objDirSearcher AsNew System.DirectoryServices.DirectorySearcher(objDirE ntry) Dim objCollSearchResult As System.DirectoryServices.SearchResultCollection Dim objlSearchResult As System.DirectoryServices.SearchResult Dim objCollResultProperty As System.DirectoryServices.ResultPropertyCollection Dim objCollResultPropertyValue As System.DirectoryServices.ResultPropertyValueCollec tion objDirSearcher.Filter = ("(&(objectClass=user)(samaccountname=" & strUserName & "))") objCollSearchResult = objDirSearcher.FindAll() SelectCase objCollSearchResult.Count Case 0 strEmailAddress = "" CaseIs > 1 ExitFunction CaseIs = 1 objlSearchResult = objCollSearchResult.Item(0) objCollResultProperty = objlSearchResult.Properties objCollResultPropertyValue = objCollResultProperty.Item("mail") strEmailAddress = objCollResultPropertyValue.Item(0) EndSelect '################################################# ######### '# forced disposing # '################################################# ######### objDirEntry.Dispose() objDirSearcher.Dispose() objCollSearchResult.Dispose() objlSearchResult = Nothing objCollResultProperty = Nothing objCollResultPropertyValue = Nothing Catch ex As System.Exception Dim strMess AsString strMess = ex.Message strEmailAddress = "" EndTry Return strEmailAddress End Function |
![]() |
| Viewing: Dev Articles Community Forums > Programming > .NET Development > Trying to access the Active Directory through VB.NET Web Forms |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|