Eric Guo's blog.cloud-mes.com

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

Query Active Directory Using Ruby Net Ldap Gems

Permalink

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 = Net::LDAP.new :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"
res=ldap.search(:base => treebase, :filter => filter)
p ldap.get_operation_result

Comments