# Ruby-ldap, invalid credentials

**URL:** <https://rubytalk.org/t/ruby-ldap-invalid-credentials/22556>\
**Category:** ruby-talk\
**Created:** [17 November 2005 17:38 UTC](https://rubytalk.org/t/ruby-ldap-invalid-credentials/22556 "2005-11-17T17:38:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Berger\_Daniel1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@Berger\_Daniel1](https://rubytalk.org/u/Berger_Daniel1)\
**Post date:** [17 November 2005 17:38 UTC](https://rubytalk.org/t/ruby-ldap-invalid-credentials/22556/1 "2005-11-17T17:38:55Z")

</div>

Hi all,

Another ldap newb question.

I can't seem to bind to the ldap server with a password. For example, this works:

# ldaptest.rb  
require "ldap"

ldap\_server = "[ldap.foo.com](http://ldap.foo.com)"  
password = "XXX"

ldap = LDAP::Conn.new(ldap\_server)  
ldap.simple\_bind("cn=djberge,dc=foo,dc=bar,dc=com")

However, if I try to password as the second argument to simple\_bind, I get an Invalid credentials (LDAP::ResultError). I'm positive the password is correct.

My main goal is to perform ldap authentication within a Rails controller. I thought I could just check success or failure on simple\_bind, but that doesn't seem to be the case.

What am I doing wrong?

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![Vladimir\_Konrad](https://avatars.discourse-cdn.com/v4/letter/v/a88e4f/32.png) [@Vladimir\_Konrad](https://rubytalk.org/u/Vladimir_Konrad)\
**Post date:** [18 November 2005 13:37 UTC](https://rubytalk.org/t/ruby-ldap-invalid-credentials/22556/2 "2005-11-18T13:37:18Z")

</div>

class LdapAdmin  
&nbsp;&nbsp;def initialize()  
&nbsp;&nbsp;&nbsp;&nbsp;@LDAP\_SERVER = '[ldap.lse.ac.uk](http://ldap.lse.ac.uk)'  
&nbsp;&nbsp;&nbsp;&nbsp;@LDAP\_BASE\_DN = 'dc=linux,dc=lse,dc=ac,dc=uk'  
&nbsp;&nbsp;end

&nbsp;&nbsp;def login\_connect?(login, password)  
&nbsp;&nbsp;&nbsp;&nbsp;# test whether the login can connect to ldap with password  
&nbsp;&nbsp;&nbsp;&nbsp;# -\> true - yes  
&nbsp;&nbsp;&nbsp;&nbsp;# -\> false - no  
&nbsp;&nbsp;&nbsp;&nbsp;require 'ldap'  
&nbsp;&nbsp;&nbsp;&nbsp;flag = false  
&nbsp;&nbsp;&nbsp;&nbsp;con = LDAP::Conn.new(@LDAP\_SERVER)  
&nbsp;&nbsp;&nbsp;&nbsp;con.set\_option(LDAP::LDAP\_OPT\_PROTOCOL\_VERSION, 3)  
&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;con.bind("cn=#{login},#{@LDAP\_BASE\_DN}", password)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;con.unbind()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flag = true  
&nbsp;&nbsp;&nbsp;&nbsp;rescue LDAP::ResultError  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flag = false  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;return flag  
&nbsp;&nbsp;end  
end

the above chunk of code works for me. the #{login} is admin and the password  
is the one specified in /etc/ldap/slapd.conf file (basically, i use it to  
log in to ldap admin account).

hope this helps,

vlad

---

<div class="post-metadata">

**Author:** ![Vladimir\_Konrad](https://avatars.discourse-cdn.com/v4/letter/v/a88e4f/32.png) [@Vladimir\_Konrad](https://rubytalk.org/u/Vladimir_Konrad)\
**Post date:** [18 November 2005 13:47 UTC](https://rubytalk.org/t/ruby-ldap-invalid-credentials/22556/3 "2005-11-18T13:47:18Z")

</div>

[http://pegacat.com/jxplorer/](http://pegacat.com/jxplorer/)

helps me a lot with ldap.

vlad
