Skip to content

Instantly share code, notes, and snippets.

@kflu
Created January 6, 2017 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kflu/ea18e097427f3d458322011025583384 to your computer and use it in GitHub Desktop.
Save kflu/ea18e097427f3d458322011025583384 to your computer and use it in GitHub Desktop.
Accessing AD through LDAP
(* Accessing AD through LDAP
Inspired by http://stackoverflow.com/a/14814508/695964
Need nuget package System.DirectoryServices
*)
#r @"./packages/System.DirectoryServices/lib/System.DirectoryServices.dll"
open System
open System.Collections
open System.DirectoryServices
let de = new DirectoryEntry() // connects to the local domain controller
// these two are optional
de.Path <- "LDAP://OU=UserAccounts,DC=foo,DC=bar,DC=baidu,DC=com" // This scopes the subsequence queries
de.AuthenticationType <- AuthenticationTypes.Secure
let s = new DirectorySearcher(de, Filter="(name=John Smith)")
let res = s.FindOne()
res.Properties.["name"] // this is always a seq
res.Properties.["name"].[0] // this is always a obj that needs to be casted at runtime
res.Properties.["name"].[0] :?> string // I know it's a string
let myMailboxGuid = Guid(res.Properties.["someBinaryField"].[0] :?> byte array)
// Display all fields (res.Properties implements IDictionary: http://stackoverflow.com/a/3267704/695964)
res.Properties |> Seq.cast<DictionaryEntry> |> Seq.iter (fun x -> printfn "%A" (x.Key, x.Value))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment