Ruby连接到LDAP技巧分享

我们在实际编写Ruby语言代码时,在实现Ruby连接到LDAP的情况下,通常都可以使用到三种方法。在这里我们将会学到其中的两种。#t#

Ruby连接到LDAP代码示例:

  1. conn = LDAP::Conn.new("rsads02.foo.com")   
  2. conn.bind("CN=username,CN=Users,DC=foo,
    DC=com","password") do |bound|   
  3. bound.search("DC=foo,DC=com", 
    LDAP::LDAP_SCOPE_SUBTREE,"(&(name=*) 
    (objectCategory=person))", 
    ['name','ipPhone']) do |user|   
  4. puts "#{user['name']} #{user['ipPhone']}"   
  5. end   
  6. end  
  7. require 'net/ldap'   
  8. ldap = Net::LDAP.new :host => 
    server_ip_address,   
  9. :port => 389,   
  10. :auth => {   
  11. :method => :simple,   
  12. :username =>"cn=manager,dc=example,dc=com",   
  13. :password => "opensesame"   
  14.  }   
  15. filter = Net::LDAP::Filter.eq( 
    "cn", "George*" )   
  16. treebase = "dc=example,dc=com"   
  17. ldap.search( :base => treebase,
     :filter => filter )do |entry|   
  18. puts "DN: #{entry.dn}"   
  19. entry.each do |attribute, values|   
  20. puts "  #{attribute}:"   
  21. values.each do |value|   
  22. puts "   --->#{value}"   
  23. end   
  24. end   
  25. end   
  26. p ldap.get_operation_result 

希望以上介绍的这些Ruby连接到LDAP的方法能够帮助大家。


分享文章:Ruby连接到LDAP技巧分享
本文URL:http://gydahua.com/article/djjcjph.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流