1
我正在嘗試搜索ldap。這是成功的身份驗證,我使用正確和不正確的密碼進行了驗證。 Ldap連接成功。然後,當我進行搜索查詢時,它無法從LDAP(AD)讀取屬性。這裏有什麼問題? ldap/AD對誰可以做什麼有任何限制/權限?在net-ldap中搜索Ldap Ruby Rails
#!/usr/bin/env ruby
require "net-ldap"
$username = String.new
class ActiveDirectoryUser
SERVER = '10.10.10.10'
PORT = 389
BASE = 'DC=mydomain,DC=com'
DOMAIN = 'mydomain.com'
def self.authenticate(login, pass)
conn = Net::LDAP.new :host => SERVER,
:port => PORT,
:base => BASE,
:auth => { :username => "#{login}@#{DOMAIN}",
:password => pass,
:method => :simple }
if conn.bind
conn.search(
:base => BASE, :filter => Net::LDAP::Filter.eq(
"sAMAccountName", login),
:attributes => %w[ givenName ], :return_result => true) do
|entry|
puts "givenName: #{entry.givenName}"
$username = entry.givenName
end
return true
else
return false
end
rescue Net::LDAP::LdapError => e
return false
end
end
if ActiveDirectoryUser.authenticate('myusername', 'mypassword')
puts "Authentication Successful! The user is "+$username
#### I get this,but blank username
else
puts "Authentication FAILED!"
end
-----------
# ./ad.rb
Authentication Successful! The user is
感謝特里的回答。 這是一個小問題。我錯過了樹形結構中的一些細節。它正在工作。