LDAP(Light-weighted Directory Access Protocol)是一種集中管理使用者與認證資訊的一種機制。
以下做法是由LDAP讀取資料後,寫到ListBox
做法一(利用child方式列舉所有項目):
Dim LdapServer As String = "LDAP://LDAP.xxx.gov.tw/" 'IP Address
Dim LdapPath As String = "o=xxx,c=tw"
LdapServer = LdapServer + LdapPath
'----------------------------------------------------------------------------------------------------------------------
'以下寫法確定可以連進 LDAP , 並找到 使用者名稱 及取出相關屬性
'----------------------------------------------------------------------------------------------------------------------
Dim LdapEntry As DirectoryEntry = New DirectoryEntry(LdapServer, "UserId", "PW", AuthenticationTypes.Anonymous)
Dim i As Integer = 0
'預設載入根目錄(底下子目錄有3個)
For Each child As DirectoryEntry In LdapEntry.Children
If child.Name = "ou=users" Then
For Each child_dt As DirectoryEntry In child.Children
Dim LdapSearcher As DirectorySearcher = New DirectorySearcher(child_dt)
Dim LdapResult_dt As SearchResultCollection = LdapSearcher.FindAll()
'LdapSearcher.PropertiesToLoad.Add("fullname")
For Each result_dt As SearchResult In LdapResult_dt
'若屬性有重複會以陣列方式儲存 ~"~任何東西都有任何可能,要自己親手試了才知道
ListBox1.Items.Add(result_dt.Properties("fullname")(0))
Next
i += 1
LdapSearcher.Dispose()
Next
ListBox1.Items.Add(ListBox1.Items.Count.ToString())
ListBox1.Items.Add(i.ToString())
End If
Next
LdapEntry.Dispose()
做法二(利用SearchResultCollection取出所有值):
Dim LdapServer As String
LdapServer = "LDAP://LDAP.vghtc.gov.tw:389/o=xxx,c=tw"
'----------------------------------------------------------------------------------------------------------------------
'以下寫法確定可以連進 LDAP , 並找到 使用者名稱 及取出相關屬性
'----------------------------------------------------------------------------------------------------------------------
'透過DirectoryEntry類別取得LDAP目錄
Dim LdapEntry As DirectoryEntry = New DirectoryEntry(LdapServer, "id", "pw", AuthenticationTypes.Anonymous)
'使用DirectorySearcher類別進行目錄查詢
Dim LdapSearcher As DirectorySearcher = New DirectorySearcher(LdapEntry, "(cn=" + TextBox1.Text + ")") ', "(ou=users)"
'取得所有目錄節點
Dim LdapResult As SearchResultCollection = LdapSearcher.FindAll()
Dim result As SearchResult
Dim myKey As String
Dim Ptname As ICollection
Dim myCollection As Object
'取出每個目錄節點之資料
For Each result In LdapResult
'取出所有的attributes居然就是沒有userpassword =..=(密碼無法自Ldap取出?)
Ptname = result.Properties.PropertyNames
'取出所有屬性欄位
For Each myKey In Ptname
'取出屬性內之所有值
For Each myCollection As Object In result.Properties(myKey)
ListBox1.Items.Add(myCollection)
Next
Next myKey
'有些資料會以byte()格式儲存,取出後須再進行轉換
'將Byte()轉換成字串)System.Text.Encoding.UTF8(感謝恆逸 鄭淑芬老師)
ListBox1.Items.Add(System.Text.Encoding.UTF8.GetString(result.Properties("fullname")(0)))
ListBox1.Items.Add(System.Text.Encoding.UTF8.GetString(result.Properties("description")(0)))
'若屬性會以ResultPropertyCollection 方式回傳 可用count屬性取得集合個數 ~"~任何東西都有任何可能,要自己親手試了才知道
'取出權限
For i As Integer = 0 To result.Properties("permissionids").Count - 1
ListBox1.Items.Add(System.Text.Encoding.UTF8.GetString(result.Properties("permissionids")(i)))
Next
Next
LdapSearcher.Dispose()
LdapEntry.Dispose()
以下為上網找到之參考資料:
如何使用表單驗證及 Visual Basic .NET 通過 Active Directory 驗證
利用 LDAP 取得AD Server 的某個群組的使用者名單
以下對LDAP介紹文件
沒有留言:
張貼留言