Eric Guo's

Hoping writing JS, Ruby & Rails and Go article, but fallback to DevOps note

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

Or for 2019 thape domain:

ldapsearch -x -LLL -D "THAPE\guochunzhong" -w "Password" -b "DC=thape,DC=com,DC=cn" -s sub -H ldap:// "samaccountname=guochunzhong" 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