ruby-on-rails - 使用 ruby​​ 的 LDAP 绑定(bind)中的错误号

标签 ruby-on-rails ldap ldapconnection

我正在使用以下代码来检查提供的参数是否正确用于绑定(bind)..

require 'rubygems'
require 'net/ldap'

ldap = Net::LDAP.new
ldap.host = your_server_ip_address
ldap.port = 389
ldap.auth "joe_user", "opensesame"
if ldap.bind
   # authentication succeeded
else
   # authentication failed
end

如果用户和密码不正确,则返回 false,但如果主机或端口号不正确,则显示错误(异常)
 no connection to server (Net::LDAP::LdapError)

我还想捕获此错误,并希望分别为 incoreect 用户/密码和不正确的端口/主机名显示错误消息。我怎样才能做到这一点

最佳答案

您可以在此处检查所有可能的异常:https://www.rubydoc.info/gems/net-ldap/Net/LDAP/Error

但是有些错误不被认为是异常,所以你必须用 get_operation_result 来获取它们。

ldap = Net::LDAP.new

begin
    ldap.host = '1.2.3.4'
    ldap.auth 'cn=config', 'myPassword'
    ldap.bind

rescue Net::LDAP::AuthMethodUnsupportedError => e
    puts "Net::LDAP::AuthMethodUnsupportedError: #{e}"

rescue Net::LDAP::BindingInformationInvalidError => e
    puts "Net::LDAP::BindingInformationInvalidError: #{e}"

rescue Net::LDAP::ConnectionError => e
    puts "Net::LDAP::ConnectionError: #{e}"

rescue Net::LDAP::ConnectionRefusedError => e
    puts "Net::LDAP::ConnectionRefusedError: #{e}"

rescue Net::LDAP::SocketError => e
    puts "Net::LDAP::SocketError: #{e}"

rescue StandardError => e
    # Timeout errors are rescued here
    puts "StandardError: #{e}"

end

# Wrong user/pass is not an exception and must be checked here
p ldap.get_operation_result

关于ruby-on-rails - 使用 ruby​​ 的 LDAP 绑定(bind)中的错误号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14434848/

相关文章:

ruby-on-rails - print + each() block - 为什么加入不起作用?

ruby-on-rails - 如何设置 ActiveStorage 以允许公共(public)和私有(private)附件

java - Sun Directory Server 无法连接 ldap 服务器

python 和 ldap 通过 SSL

ruby-on-rails - has_many 依赖 : destroy is searching for a wrong column name

ruby-on-rails - Rails 嵌套属性 : require at least two records

ruby - 如何使用事件目录和 ruby​​ 脚本检查用户凭据

java - Java 中的搜索结果

java - 如何从 LDAP 查询多个用户

java - Java 应用程序如何无缝地接受服务器证书?