Eric Guo's

in HTML5, JavaScript, Ruby & Rails, Python, and Cloud MES!

Query Active Directory Using Ruby Net Ldap Gems


I’m trying devise_ldap_authenticatable during 2013 CNY, after spending a lot of time, found below script is quite usful if you want to using LDAP authenticate but found something wrong and start trying to debug.

Confirm first command line ldap works good
ldapsearch -x -LLL -D "SDCORP\mes.service" -w "Password" -b "DC=sdcorp,DC=global,DC=sandisk,DC=com" -s sub -H ldap://cvpcdcip04 "cn=Eric Guo" cn mail displayName samaccountname
Using ruby gems to test
require 'net/ldap'
ldap = :host => 'cvpcdcip04',
:port => 389,
:auth => {
:method => :simple,
:username => "MES Service",
:password => "Password"
filter = Net::LDAP::Filter.eq("cn", "MES Service")
# treebase must exactly match, otherwise can not found the entity
treebase = "DC=sdcorp,DC=global,DC=sandisk,DC=com" => treebase, :filter => filter)
p ldap.get_operation_result