title: Ldap search in net-ldap Ruby Rails
body: <p>I am trying to search ldap. This is doing successful authentication, I verified using a correct and incorrect password. Ldap connection is successful. Then when I make a search query it cannot read attributes from LDAP (AD). What is wrong here? Does ldap/AD has any restrictions/permissions on who can what? </p>
  
  <pre><code>#!/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 =&gt; SERVER,
                           :port =&gt; PORT,
                           :base =&gt; BASE,
                           :auth =&gt; { :username =&gt; "#{login}@#{DOMAIN}",
                                      :password =&gt; pass,
                                      :method =&gt; :simple }
      if  conn.bind
          conn.search(
          :base =&gt; BASE, :filter =&gt; Net::LDAP::Filter.eq(
  "sAMAccountName", login ),
          :attributes =&gt; %w[ givenName ], :return_result =&gt; true) do
  |entry|
          puts "givenName: #{entry.givenName}"
          $username = entry.givenName
          end
          return true
  
      else
        return false
      end
    rescue Net::LDAP::LdapError =&gt; 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
  </code></pre>
  
  <hr>
  
  <p>Thanks Terry for the answer. 
  It was a small issue. I was missing some details in the treebase. It is working now. </p>