Using ASP.Net to Get User Information from AD

D

Derek Martin

Hey list, got this code running in a webform:

Dim DSESearcher As System.DirectoryServices.DirectorySearcher = New
System.DirectoryServices.DirectorySearcher
Dim RootDSE As String = DSESearcher.SearchRoot.Path
RootDSE = RootDSE.Insert(7, "ou=users,ou=myusers,")
Dim myDE As DirectoryEntry = New DirectoryEntry(RootDSE)
Dim myEntries As DirectoryEntries = myDE.Children
Dim thisentry As DirectoryEntry
thisentry = myEntries.Find("cn=" & lastname)
Label1.Text = thisentry.Name

Obviously, this won't work because it's running as the ASP.Net user. How do
I go about getting another or hardcoded user to authenticate with
DSESearcher?

Thanks!
Derek
 
S

scorpion53061

See
http://support.microsoft.com/default.aspx?scid=kb;en-us;329986

Imports System
Imports System.Diagnostics
Imports System.DirectoryServices
Imports System.Xml

Namespace ASPNet_App
'/ <summary>
'/
'/ </summary>
Public Class ADSIUtil
Protected m_strErrors As String = ""
Protected m_strRoot As String = ""
Protected m_obDirEnTry As DirectoryEnTry
Public Sub New()

End Sub


Public ReadOnly Property Errors() As String
Get
Return m_strErrors
End Get
End Property


Public Property RootNode() As String
Get
Return m_strRoot
End Get
Set (ByVal Value As String)
m_strRoot = value
End Set
End Property


Public Function Initialize(ByVal strRoot As String) As Boolean
If m_strRoot = Nothing Or m_strRoot.Length = 0 Then
RootNode = strRoot
End If

Try
m_obDirEnTry = New DirectoryEnTry(strRoot)
'--*' foreach (String propName in
m_obDirEnTry.Properties.PropertyNames)
' {
' foreach (Object value in m_obDirEnTry.Properties(propName))
' {
' Trace.WriteLine("name=" + propName + " value=" + value)
' }
' }
' */
Catch ex As Exception
Trace.WriteLine(ex.Message)
Return False
End Try
Return True
End Function


Public Function GetUserSchema(ByVal strLogin As String) As XmlDocument
Dim userDataDoc As XmlDocument = Nothing
' Make sure that we have a root node specified.
If m_strRoot = Nothing Or m_strRoot.Length = 0 Then
m_strErrors += "Root Node not initializes"
Return userDataDoc
End If

Dim results As SearchResultCollection
Dim srch As DirectorySearcher = New DirectorySearcher(m_obDirEnTry)
srch.Filter = ("(cn=" + strLogin + ")")

Try
results = srch.FindAll()
Catch ex As NotSupportedException
m_strErrors += ex.Message
Trace.WriteLine(ex.Message)
Return userDataDoc
Catch ex As Exception
m_strErrors += "\n"
m_strErrors += ex.Message
Trace.WriteLine(ex.Message)
Return Nothing
End Try

Try
Dim result As SearchResult
For Each result In results
Dim coll As ResultPropertyCollection = result.Properties
Dim user As ADSIUser = New ADSIUser()
If False = user.Initialize(coll) Then
m_strErrors += "\n"
m_strErrors += "Failed to initialize the ADSI object"
Trace.WriteLine("Failed to initialize the ADSI object")
Return Nothing
End If

If user.NumProperties > 0 Then
userDataDoc = user.GetUserProperties()
End If
Next
Catch ex As Exception
m_strErrors += "\n"
m_strErrors += ex.Message
Trace.WriteLine(ex.Message)
Return Nothing
End Try

Return userDataDoc
End Function

'/ <summary>
'/
'/ </summary>
'/ <param name="strUser"></param>
'/ <returns></returns>
Private Function DumpSchema(ByVal strUser As String) As Boolean
If m_strRoot.Length = 0 Then
Return False
End If

Dim results As SearchResultCollection
Dim srch As DirectorySearcher = Nothing
Try
'DirectoryEntry schemaEntry = m_obDirEntry.SchemaEntry;
'Trace.WriteLine(schemaEntry.Children.ToString());
srch = New DirectorySearcher(m_obDirEnTry)
srch.Filter = ("(cn=" + strUser + ")")
results = srch.FindAll()

Dim result As SearchResult
For Each result In results
Dim coll As ResultPropertyCollection = result.Properties
Dim nItems As Integer = coll.Count
Dim myKey As String
For Each myKey In coll.PropertyNames
Dim tab As String = " "
Trace.WriteLine(myKey + " = ")
Dim myCollection As Object
For Each myCollection In coll(myKey)
Trace.WriteLine(tab + myCollection)
Next
Next
Next

Catch ex As Exception
Trace.WriteLine(ex.Message)
End Try

Return True
End Function
End Class
End Namespace
 

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