VB.net LDAP(active directory) üzerinden SID değerini alıp Stringe dönüştürmek

‘SecurityIdentifier için gerekli kütüphane
Imports System.Security.Principal
Imports System.Text
Imports System.DirectoryServices
Private Sub adkullaniciadi_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles adkullaniciadi.LostFocus
sid.Text = “”
Dim objDe As New DirectoryEntry(”LDAP://127.0.0.1¨, “LDAP KULLANICIADI”, “LDAP SIFRE”)
Dim searcher As New DirectorySearcher(objDe)
searcher.Filter = “(&(objectClass=user)(SAMAccountName=” + adkullaniciadi.Text + “))”
searcher.PropertiesToLoad.Add(”objectSid”)
Dim Results As SearchResultCollection = searcher.FindAll()
For Each Result As SearchResult In Results
Dim Rpc As ResultPropertyCollection = Result.Properties
For Each [Property] As String In Rpc.PropertyNames
For Each Value As Object In Rpc([Property])
sid.Text = SIDtoString(Value)
Next
Next
Next
If sid.Text = “” Then
MsgBox(”Active Directory Kullanıcı Adını Kontrol Edin”, MsgBoxStyle.Critical)
End If
End Sub
Public Shared Function SIDtoString(ByVal sidBinary As Byte()) As String
Dim sid As New SecurityIdentifier(sidBinary, 0)
Return sid.ToString()
End Function