# HOWTO: Authenticating with a Windows Active Directory server

**URL:** <https://www.ruby-forum.com/t/howto-authenticating-with-a-windows-active-directory-server/163901>\
**Category:** Ruby\
**Created:** [March 26, 2009, 12:05am UTC](https://www.ruby-forum.com/t/howto-authenticating-with-a-windows-active-directory-server/163901 "2009-03-26T00:05:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [March 26, 2009, 12:05am UTC](https://www.ruby-forum.com/t/howto-authenticating-with-a-windows-active-directory-server/163901/1 "2009-03-26T00:05:22Z")

</div>

I have a Ramaze-based web application at work. I wanted it to  
integrate with the Active Directory server for the company, so that no  
one would have to create accounts for new users, and the same password  
you used for your desktop would work for the application. (The web  
server happens to be accessible only on the intranet, so there was no  
security issue with passwords being sent in plaintext; no need for  
https on the server.)

Anyhow, after a few failed attempts, the final code for simply  
validating the username/password was so simple that I thought I would  
share it.

# gem install ruby-net-ldap

require ‘net/ldap’

# Sent from an HTML form; the “request” object here is from Ramaze

# Email must have the company domain, e.g. “removed\_email\_address@domain.invalid”

email, pass = request[:email], request[:password]

ldap = Net::LDAP.new(  
# There’s convention for companies to use [ldap.company.com](http://ldap.company.com);  
# Thankfully, mine uses this, so I didn’t have to bug IT to  
# figure out where the Active Directory server was.  
:host=\>‘[ldap.acmetools.com](http://ldap.acmetools.com)’,  
:auth=\>{  
:method=\>:simple,  
:username=\>email,  
:password=\>password  
}  
)

if ldap.bind  
# AD authentication succeeded; the email/password combo is valid!  
…  
end

I gather that some Active Directory installations require you to  
connect over SSL. Mine didn’t, so I didn’t need to specify the  
alternate port or the encryption mode. I also found some code that  
uses an LDAP query for the username, e.g. :username=\>“cn=#  
{username},cn=Users,dc=acmetools,dc=com”. Try as I might, though, I  
couldn’t make any form of this work with my company’s AD server. But  
the above code worked like a charm, and so simply. 🙂

Hope this helps someone.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [April 21, 2009, 6:14pm UTC](https://www.ruby-forum.com/t/howto-authenticating-with-a-windows-active-directory-server/163901/2 "2009-04-21T18:14:29Z")

</div>

This is a really good HowTo and I used something similar in my Rails  
application. I was curious if you had any code snippets on how to change  
the Active Directory password using the ruby-net-ldap gem? Any help you  
could provide would be greatly appreciated.
