Learning F# and Active Directory Fun

This past month I started work on a new web application. Of course, with any new project, I like to throw some chaos into the mix and try learning either a new framework or language. This time, I decided to write the entire backend of it in F#. I know nothing about F#, so this should be interesting.

After spending many years of always writing backend code in C#, this is probably the most chaos I’ve ever actually thrown at a project. With that said, I’m only a few weeks into learning F# and I find it a fascinating change of pace. Though as with any new learning experience, I quite often have ten extra tabs open to various language references and Stack Overflow pages.

One thing I struggled with this week was, while likely painfully obvious to seasoned users of F#, a bit of a headache to me. How do I take an object and cast it to a byte[]? When querying Active Directory for a user’s “thumbnailPhoto”, the variable comes back as an obj, though through debugging you can clearly see it’s a byte[].

There were multiple suggestions I found, but it wasn’t until earlier today that I found the solution, which is to use the downcasting operator :?>, which honestly looks like an emoji with a big nose and a sharp smile. I’m sure there’s logic to its design, but it’s forever the downcast emoji to me.

Anyways, someone on Stack Overflow posted an excellent example (I’ll update this once I can find it again), and I was able to reference their suggestion in order to pull off the conversion and pass the data correctly out to the web interface (still written in C#).

So in case anyone else happens to run into the same issue I did, below is the solution I came up with to query, downcast, and return the value. Again please understand that, as of this post, I’m only two weeks into writing F#. If you have an even better (simpler?) solution, please feel reach out to me. The next improvement I plan on doing to this snippet is to have it return a “generic picture” in case the user has no photo located in active directory.

    // Query active directory (using sAMAccountName) and return a thumbnail photo
    // https://gist.github.com/bradmb/3961b39f7c4beec7a5fb

    open System.DirectoryServices

    /// Gets a user's thumbnail photo from active directory. Returns null if no image found.
    let ActiveDirectoryImageFor(samAccount:string):byte[] =
        use adSearcher = new DirectorySearcher()
        adSearcher.Filter <- "(&(objectClass=user) (sAMAccountName=" + samAccount + "))"

        let adResult = adSearcher.FindOne()
        if adResult = null
            then
                null
            else 
                use adUser = new DirectoryEntry(adResult.Path)
                let adJpeg = adUser.Properties.["thumbnailPhoto"]
                let adImageData = if (adJpeg.Value :? byte[]) then adJpeg.Value :?> byte[] else null

        adImageData

Leave a Reply

Your email address will not be published. Required fields are marked *