My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
LDAP in Ruby is better than LDAP in C#/.NET. Looking at it, I can't say it's much different minus the cruft from .NET.

Lightweight? Seriously?

Experiencing it while actually writing code, it's very different. I can't explain it, except to show it to you and tell you try it.

Ruby LDAP code is at github even though existing solutions with good examples point you to what are now broken links.

To install (despite README.txt saying otherwise):

gem install ruby-net-ldap

And here's some LDAP login/authorization/auth code:
require 'rubygems'

require 'net/ldap'

ldap = Net::LDAP.new
ldap.host = "ldap.example.com"

ldap.port = "389"

username = "human_interest_stories"
password = "obituary"

ldap.auth "uid=#{username},ou=users,dc=example,dc=com", password

is_authorized = ldap.bind # returns true if auth works, false otherwise (or throws error if it can't connect to the server)

#searching the LDAP from Damana (linked above too)

filter = Net::LDAP::Filter.eq( "uid", username )
#attrs = ["ou" , "objectClass"] # you can specify attributes
attrs = []

ldap.search( :base => "ou=users,dc=example,dc=com", :attributes => attrs, :filter => filter, :return_result => true ) do |entry|
    puts entry.dn
    entry.attribute_names.each do |n|
        puts "#{n} = #{entry[n]}"

    end
end

Hope it helps.

Hey! Why don't you make your life easier and subscribe to the full post or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!


Comments
Leave a comment

Great post, I've been looking for a couple of ours for a snippet for connecting to Active Directory from ruby and your post did the trick. :)

Posted by Ahmed El.Hussaini on Feb 01, 2010 at 05:02 AM UTC - 5 hrs

This helped me greatly. Thanks for the easy to understand example.

Posted by Jason on Jun 22, 2012 at 09:57 AM UTC - 5 hrs

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)
Website

Comment:

Subcribe to this comment thread
Remember my details
Google
Web CodeOdor.com

Me
Picture of me

Topics
.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and C++ (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Resources
Agile Manifesto & Principles
Principles Of OOD
ColdFusion
CFUnit
Ruby
Ruby on Rails
JUnit



RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner