Ldap没与Devise合作

我正在尝试使用Devise和Ldap然而我似乎在使用ldap设计的初始设置中做错了我已经在使用db auth设计但是,我想切换并使用我现有的AD。 任何帮助将不胜感激!

我有这个只使用LDAP的测试脚本,它运行得很好

require 'net/ldap' class ActiveDirectoryUser SERVER = 'myactivedir.mydomain.com' PORT = 389 BASE = 'DC=mydomain,DC=com' DOMAIN = 'mydomain.com' def self.authenticate(login, pass) return false if login.empty? or pass.empty? conn = Net::LDAP.new :host => SERVER, :port => PORT, :base => BASE, :auth => { :username => "#{login}@#{DOMAIN}", :password => pass, :method => :simple } if conn.bind and user = conn.search(:filter => "sAMAccountName=#{login}").first return user else return nil end rescue Net::LDAP::LdapError => e return false end end 

我用上面的代码运行它,它给了我测试的所有属性

  irb(main):003:0> user = ActiveDirectoryUser.authenticate('test','test12345') => #["CN=test,CN=Users,DC=mydomain,DC=com"], :objectclass=>["top", "person", "organizationalPerson", "user"], :cn=>["test"], :samaccountname=>["test"].......keeps going 

如果我使用错误的密码进行用户测试我得到了这个,所以我知道它正常运行auth。

  irb(main):002:0> ActiveDirectoryUser.authenticate('test','test123') => nil 

但是,当我尝试使用设计进行相同的设置时,它总会返回此信息。

  LDAP: LDAP dn lookup: sAMAccountName=test LDAP: LDAP dn lookup: sAMAccountName=test LDAP: LDAP search for login: sAMAccountName=test LDAP: LDAP search for login: sAMAccountName=test LDAP: LDAP search yielded 0 matches LDAP: LDAP search yielded 0 matches LDAP: Authorizing user sAMAccountName=test,dc=mydomain,dc=com LDAP: Authorizing user sAMAccountName=test,dc=mydomain,dc=com LDAP: Not authorized because not authenticated. LDAP: Not authorized because not authenticated. 

这是我的devise.rb配置 – >

 Devise.setup do |config| # ==> LDAP Configuration config.ldap_logger = true # config.ldap_create_user = false # config.ldap_update_password = true config.ldap_config = "#{Rails.root}/config/ldap.yml" #config.ldap_auth_username_builder = Proc.new() {|attribute, login, ldap| "#{login}@mydomain.com"} tried this still no luck...... # config.ldap_check_group_membership = false # config.ldap_check_group_membership_without_admin = false # config.ldap_check_attributes = false # config.ldap_use_admin_to_bind = false # config.ldap_ad_group_check = false 

这是我的config / ldap.yml

 development: host: myactivedir.mydomain.com domain: mydomain.com port: 389 attribute: sAMAccountName base: dc=mydomain,dc=com 

在config / devise.rb中找出它我包含了这个并且presto它工作。

 config.ldap_auth_username_builder = Proc.new() {|attribute, login, ldap| "#{login}@mydomain.com"}