Ruby LDAP Authentication with net/ldap

I’m working on a Rails project that requires LDAP authentication.

I searched the net for a long time, but finally got something to work for me.

…  just use your own username, password & hostname.

Note that the username and password should be contained in SINGLE QUOTES unless you have some reason to use double quotes…. that messed me up for a while.

<pre>
require ‘rubygems’
require ‘net/ldap’

user = ‘DOMAIN_NAME\my_username’
pass = ‘my_password’
ldap = Net::LDAP.new :host => “x.x.x.x”, :port => 389, :auth => {:method => :simple, :username => user, :password => pass}
p (ldap.bind) ? “Authorization Succeeded!” : “Authorization Failed: #{ldap.get_operation_result.message}”
</pre>

Advertisements