這是我一年半前為了讓人測驗可不可以連AD寫的。
開發環境:Window XP
開發工具:Visual Stutio 2003(後來用Visual Stutio 2005重新編譯過)
開發語言:VB.NET
開發時間:95年1月
源始檔下載
執行畫面
程式說明
.NET跟LDAP(註一)是使用System.DirectoryServices.DirectoryEntry類別
建構式的格式如下
Dim ad As New System.DirectoryServices.DirectoryEntry("LDAP://" & domain, username, password)
當連結正常後再呼叫ShowAD取出子節點資訊
註一LDAP(Lightweight Directory Access Protocol)輕型目錄訪問協議
開發環境:Window XP
開發工具:Visual Stutio 2003(後來用Visual Stutio 2005重新編譯過)
開發語言:VB.NET
開發時間:95年1月
源始檔下載
執行畫面
程式說明
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim domain As String = Me.TextBox1.Text
Dim username As String = Me.TextBox2.Text
Dim password As String = Me.TextBox3.Text
Try
Dim ad As New System.DirectoryServices.DirectoryEntry("LDAP://" & domain, username, password)
st += ad.Name & vbCrLf
ShowAD(ad, 0)
Catch ex As Exception
MessageBox.Show("輸入錯誤或無法讀取AD")
Exit Sub
End Try
MessageBox.Show("測試正常" + vbCrLf + st)
Application.Exit()
End Sub
Sub ShowAD(ByVal ad As System.DirectoryServices.DirectoryEntry, ByVal level As Integer)
For Each adChildren As System.DirectoryServices.DirectoryEntry In ad.Children
If Me.ComboBox1.SelectedIndex = -1 Then
st &= Microsoft.VisualBasic.Strings.StrDup(level, "-") & adChildren.Name & ";" & adChildren.SchemaClassName & vbCrLf
If Me.CheckBox1.Checked = True Then
For Each ItemName As String In adChildren.Properties.PropertyNames
For Each items As Object In adChildren.Properties(ItemName)
st &= Microsoft.VisualBasic.Strings.StrDup(level + 1, "-") & ItemName & ":"
Try
st &= items & vbCrLf
Catch ex As Exception
st &= "無法繫結" & vbCrLf
End Try
Next
Next
End If
ElseIf adChildren.SchemaClassName = Me.ComboBox1.SelectedItem Then
st &= Microsoft.VisualBasic.Strings.StrDup(level, "-") & adChildren.Name & ";" & adChildren.SchemaClassName & vbCrLf
If Me.CheckBox1.Checked = True Then
For Each ItemName As String In adChildren.Properties.PropertyNames
For Each items As Object In adChildren.Properties(ItemName)
st &= Microsoft.VisualBasic.Strings.StrDup(level + 1, "-") & ItemName & ":"
Try
st &= items & vbCrLf
Catch ex As Exception
st &= "無法繫結" & vbCrLf
End Try
Next
Next
End If
End If
ShowAD(adChildren, level + 1)
Next
End Sub
Dim domain As String = Me.TextBox1.Text
Dim username As String = Me.TextBox2.Text
Dim password As String = Me.TextBox3.Text
Try
Dim ad As New System.DirectoryServices.DirectoryEntry("LDAP://" & domain, username, password)
st += ad.Name & vbCrLf
ShowAD(ad, 0)
Catch ex As Exception
MessageBox.Show("輸入錯誤或無法讀取AD")
Exit Sub
End Try
MessageBox.Show("測試正常" + vbCrLf + st)
Application.Exit()
End Sub
Sub ShowAD(ByVal ad As System.DirectoryServices.DirectoryEntry, ByVal level As Integer)
For Each adChildren As System.DirectoryServices.DirectoryEntry In ad.Children
If Me.ComboBox1.SelectedIndex = -1 Then
st &= Microsoft.VisualBasic.Strings.StrDup(level, "-") & adChildren.Name & ";" & adChildren.SchemaClassName & vbCrLf
If Me.CheckBox1.Checked = True Then
For Each ItemName As String In adChildren.Properties.PropertyNames
For Each items As Object In adChildren.Properties(ItemName)
st &= Microsoft.VisualBasic.Strings.StrDup(level + 1, "-") & ItemName & ":"
Try
st &= items & vbCrLf
Catch ex As Exception
st &= "無法繫結" & vbCrLf
End Try
Next
Next
End If
ElseIf adChildren.SchemaClassName = Me.ComboBox1.SelectedItem Then
st &= Microsoft.VisualBasic.Strings.StrDup(level, "-") & adChildren.Name & ";" & adChildren.SchemaClassName & vbCrLf
If Me.CheckBox1.Checked = True Then
For Each ItemName As String In adChildren.Properties.PropertyNames
For Each items As Object In adChildren.Properties(ItemName)
st &= Microsoft.VisualBasic.Strings.StrDup(level + 1, "-") & ItemName & ":"
Try
st &= items & vbCrLf
Catch ex As Exception
st &= "無法繫結" & vbCrLf
End Try
Next
Next
End If
End If
ShowAD(adChildren, level + 1)
Next
End Sub
.NET跟LDAP(註一)是使用System.DirectoryServices.DirectoryEntry類別
建構式的格式如下
Dim ad As New System.DirectoryServices.DirectoryEntry("LDAP://" & domain, username, password)
當連結正常後再呼叫ShowAD取出子節點資訊
註一LDAP(Lightweight Directory Access Protocol)輕型目錄訪問協議