find info from AD

  • Thread starter Thread starter Sakinah
  • Start date Start date
S

Sakinah

Hello guys,

I have write code below to get full name from Active Directory (AD) based on
windows user name (strUserID). When I wrote in Windows Form, it return
value. But I need to show in Web Form but display message error . any idea?

Public Function GetUserInfo(ByVal UserID As String)

Dim ADEntry As New DirectoryServices.DirectoryEntry("LDAP://myDomain")

Dim ADSearch As New System.DirectoryServices.DirectorySearcher(ADEntry)

Dim ADSearchResult As System.DirectoryServices.SearchResult

ADSearch.Filter = ("(samAccountName=" & UserID & ")")

ADSearch.SearchScope = SearchScope.Subtree

Dim UserFound As SearchResult = ADSearch.FindOne()

If Not IsNothing(UserFound) Then

MsgBox(UserFound.GetDirectoryEntry().Properties.Item("name").Value)

End If

End Function

An operations error occurred
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: An
operations error occurred

Source Error:

Line 62: Dim UserFound As SearchResult = ADSearch.FindOne()
 
¤ Hello guys,
¤
¤ I have write code below to get full name from Active Directory (AD) based on
¤ windows user name (strUserID). When I wrote in Windows Form, it return
¤ value. But I need to show in Web Form but display message error . any idea?
¤
¤ Public Function GetUserInfo(ByVal UserID As String)
¤
¤ Dim ADEntry As New DirectoryServices.DirectoryEntry("LDAP://myDomain")
¤
¤ Dim ADSearch As New System.DirectoryServices.DirectorySearcher(ADEntry)
¤
¤ Dim ADSearchResult As System.DirectoryServices.SearchResult
¤
¤ ADSearch.Filter = ("(samAccountName=" & UserID & ")")
¤
¤ ADSearch.SearchScope = SearchScope.Subtree
¤
¤ Dim UserFound As SearchResult = ADSearch.FindOne()
¤
¤ If Not IsNothing(UserFound) Then
¤
¤ MsgBox(UserFound.GetDirectoryEntry().Properties.Item("name").Value)
¤
¤ End If
¤
¤ End Function
¤
¤ An operations error occurred
¤ Description: An unhandled exception occurred during the execution of the
¤ current web request. Please review the stack trace for more information
¤ about the error and where it originated in the code.
¤
¤ Exception Details: System.Runtime.InteropServices.COMException: An
¤ operations error occurred
¤
¤ Source Error:
¤
¤ Line 62: Dim UserFound As SearchResult = ADSearch.FindOne()


Did you turn off Anonymous authentication for your web application?


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
I have put this configuration in web.config, but stil gor same error

AUTHENTICATION
<authentication mode = "Windows"/>

Authorization
<allow users="*"/> <!-- Allow all users -->
 
¤
¤
¤ I have put this configuration in web.config, but stil gor same error
¤
¤ AUTHENTICATION
¤ <authentication mode = "Windows"/>
¤
¤ Authorization
¤ <allow users="*"/> <!-- Allow all users -->
¤

Yes, but in IIS did you turn off anonymous authentication for your application?


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
I have write code below to get full name from Active Directory (AD) based on
windows user name (strUserID). When I wrote in Windows Form, it return
value. But I need to show in Web Form but display message error . any idea?

Public Function GetUserInfo(ByVal UserID As String)
Dim ADEntry As New DirectoryServices.DirectoryEntry("LDAP://myDomain")

Usually, in a ASP.NET environment, you need to

a) specify the full LDAP path, including the server you are going to
hit (not just the domain), something like:

LDAP://myDC01.myDomain.com/dc=myDomain,dc=com

b) supply usable credentials for the call (can't use "anonymous"
binding since the ASP.NET app is running under the security context of
the ASP user, not your own user account). So you need to specify user
name and password in your "new DirectoryEntry()" call.

Marc
================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
¤
¤
¤ Yes, I did turn off anonymous authentication in IIS.
¤

I think Marc might be correct. The domain reference when using the LDAP (Active Directory) provider
is different from the WinNT provider. If you don't know what the default naming context is for your
Active Directory domain try the following:

Dim RootDSE As New DirectoryServices.DirectoryEntry("LDAP://RootDSE")
Dim DomainDN As String = RootDSE.Properties("DefaultNamingContext").Value
Dim ADEntry As New DirectoryServices.DirectoryEntry("LDAP://" & DomainDN)


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
Marc, stil got error under although i have put full LDAP path

Dim UserFound As SearchResult = ADSearch.FindOne()
-----

Paul,cannot get default name. error under

Dim DomainDN As String =
RootDSE.Properties("DefaultNamingContext").Value
 
Marc, stil got error under although i have put full LDAP path
Dim UserFound As SearchResult = ADSearch.FindOne()

Show me the WHOLE code again, and show me the FULL LDAP PATH, as you
use it, too - thanks!

Marc

================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
this is my full code to get full name in AD:


Public Function GetUserInfo(ByVal UserID As String)

Dim ADEntry As New
DirectoryServices.DirectoryEntry("LDAP://dc=ambersoft-internal,dc=local"
)
Dim ADSearch As New
System.DirectoryServices.DirectorySearcher(ADEntry)

Dim ADSearchResult As System.DirectoryServices.SearchResult
ADSearch.Filter = ("(samAccountName=" & UserID & ")")
ADSearch.SearchScope = SearchScope.Subtree

'error here
Dim UserFound As SearchResult = ADSearch.FindOne()
If Not IsNothing(UserFound) Then
MsgBox
UserFound.GetDirectoryEntry).Properties.Item("name").Value)
End If

End Function
 
Public Function GetUserInfo(ByVal UserID As String)
Dim ADEntry As New
DirectoryServices.DirectoryEntry("LDAP://dc=ambersoft-internal,dc=local")
Dim ADSearch As New
System.DirectoryServices.DirectorySearcher(ADEntry)
ADSearch.Filter = ("(samAccountName=" & UserID & ")")
ADSearch.SearchScope = SearchScope.Subtree

'error here
Dim UserFound As SearchResult = ADSearch.FindOne()

And what exactly does the error say? The code looks okay from my
point of view - the only thing I can see is that the LDAP path used
for the "ADEntry" object would be invalid - is the "ADEntry" object
created okay? Or is it null / Nothing??

Also, if you want to use just a few select properties afterwards, it
would be helpful to add those to the list of attributes to the loaded
by the directory searcher, and then reference that directly, instead
of going the long route of calling .GetDirectoryEntry and then using
that DirEntry for your properties:

ADSearch.PropertiesToLoad.Add("name");
[and later on]
MsgBox UserFound.Properties.Item("name").Value


Marc
================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
Back
Top