2010-05-19 60 views

回答

3

ActiveDirectory是LDAP的實現。您可以使用RubyLDAP寶石與AD集成。我目前使用這個gem從RHEL服務器連接到Windows域控制器。

gem install ruby-ldap 
+0

您好jamin,我正在使用ruby-ldap並在rails3應用程序中遇到問題,請問您可以看看這個問題嗎? http://stackoverflow.com/questions/11979920/ruby-ldap-gem-not-work-in-rails3-app-but-work-in-rails-console – 2012-08-16 02:36:41

1

對於Ruby的LDAP綁定是相當不錯的 - 不完全美麗,但他們工作得很好。當然,您可以訪問ActiveDirectory 作爲的LDAP服務器。我從來沒有嘗試任何Ruby的ActiveDirectory綁定。

4

我用net-ldap寶石認證和工作查詢ActiveDirectory的服務器。它運作良好。以下是一些示例代碼,用於驗證用戶的登錄憑據並獲取其全名。

def name_for_login(email, password) 
    email = email[/\A\w+/].downcase # Throw out the domain, if it was there 
    email << "@mycompany.com"  # I only check people in my company 
    ldap = Net::LDAP.new(
    host: 'ldap.mycompany.com', # Thankfully this is a standard name 
    auth: { method: :simple, email: email, password:password } 
) 
    if ldap.bind 
    # Yay, the login credentials were valid! 
    # Get the user's full name and return it 
    ldap.search(
     base:   "OU=Users,OU=Accounts,DC=mycompany,DC=com", 
     filter:  Net::LDAP::Filter.eq("mail", email), 
     attributes: %w[ displayName ], 
     return_result:true 
    ).first.displayName.first 
    end 
end