Using VB.Net To Encrypt LDAP MD5 Passwords

Wednesday, February 11th, 2009

When passwords are stored in an LDAP directory they are stored encrypted. LDAP supports different types of hashing, but I chose to use MD5.  The LDAP implementation is to use Base 64 encoding. Below is a simple function I use to convert the plain text password into a hashed string ready to be added to LDAP.

Public Shared Function EncryptPassword(ByVal value As String) As String

        Dim cryptProvider As New MD5CryptoServiceProvider
        Dim b As Byte() = Text.UTF8Encoding.UTF8.GetBytes(value)
        Dim encoded As Byte() = cryptProvider.ComputeHash(b)
        Dim hash As String = Convert.ToBase64String(encoded)

        Return hash

End Function

Deleting LDAP Entries

Thursday, August 14th, 2008

Following on from my popular post on searching LDAP directories I thought I’d follow up with a simple piece of code that allows you to delete entries.

There are two ways to remove an entry. The first way is to search for a node an remove it, along with all of its children. The second way is to search for a node and remove one or more of its children. (more…)

Working With LDAP In VB.Net

Friday, June 13th, 2008

I wrote a post a while ago dealing with an error that VB can throw when dealing with an LDAP connection, you can find it here. Because this post has proved popular with people searching for the error code on Google, I thought I’d put together a quick post on using LDAP in VB.Net.

(more…)

System.DirectoryEntry – Adding new objects

Monday, September 3rd, 2007

I have recently been playing around with Apaches Directory Service, creating custom schema types and the like but came across a problem.

In VB.Net if I tried to add a new child to a directory entry, of a custom schema type (in this case in inherited from the person class) it would throw an exception when I tried to commit changes. On closer inspection I noticed that instead of creating an object of my custom schema class, it was trying to create an object of the person class, and the error being thrown by the LDAP server was because some of the properties I was trying to set did not exist for the Person class.

I managed to get around this by adding a property ‘objectClass’ and setting the value to the type of my custom schema class. This means that when I call commit changes, the LDAP server can validate it against that class.

AddUpdateProperty(newUser, "objectClass", "myUserClass")
AddUpdateProperty(newUser, "mail", user.Email)
AddUpdateProperty(newUser, "uid", user.ID)

Technorati Tags: , ,

System.DirectoryEntry Unknown error (0×80005000)

Friday, August 31st, 2007

I have recently encountered this really unhelpful error message in VB.Net whilst trying to code a connection to an LDAP server.

After much head banging I found the solution! It’s all down to the path you supply to the constructor:

Old Connection (doesn’t work):

Using dir As New DirectoryEntry(ldap://yourserver:port)
	' Your Code
End Using

New Connection (does work):

Using dir As New DirectoryEntry(LDAP://yourserver:port)
	' Your Code
End Using

Spot the difference? That’s right! the protocol part (LDAP) is case sensitive. Changing to uppercase works like a charm.

Update: I have posted an article and sample code on working with LDAP here.

Technorati Tags: , , ,


foreandaft