4

我正在尝试使用已使用net-ldapgem 设置的密码创建一个 AD 帐户。我能够很好地连接到服务器。而且我也可以在不传递:unicodepwd属性的情况下添加新用户,但是在创建新用户时没有设置密码。当我传递该属性时,不会创建用户并且它失败error code 53并显示以下消息Unwilling to perform。如果我在创建用户密码后尝试替换用户密码,也会遇到同样的错误。我遇到了许多潜在的答案,但没有一个对我有用。

def initialize

        @client = Net::LDAP.new
        @client.host = server_ip
        @client.base = base
        @client.port = 389
        @client.auth(username, password)

        if @client.bind
            puts "Connected"

            add("TEST", "JEST", "testjest")
        else
            puts "Not Connected"
            display_error   
        end
    end

def add(first_name, last_name, username)
        dn = dn_value

        attrs = {
            :objectclass => ["top", "person", "organizationalPerson", "user"],
            :cn => fullname(first_name, last_name),
            :sn => last_name.capitalize,
            :givenname => first_name.capitalize,
            :displayname => fullname(first_name, last_name),
            :name => fullname(first_name, last_name),
            :samaccountname => username,
            :unicodePwd => '"password"'.encode("utf-16")
        }
        @client.add(:dn => dn, :attributes => attrs)



        if @client.get_operation_result.code != 0
            puts "Failed to add user #{fullname(first_name, last_name)}"
            display_error
        else
            puts "Added user #{fullname(first_name, last_name)}"
        end
    end

当我创建用户并且不必通过 gui 访问它来更新密码时,如何为用户设置密码?任何帮助表示赞赏

谢谢

更新

一旦我以不同的方式对字符串进行编码并连接到 SSL 端口 636 而不是默认端口 389,我就能够让它工作。使用encode是问题,似乎它错误地编码了密码。

这是我的新连接

@client = Net::LDAP.new
@client.host = server_ip
@client.base = base
@client.port = 636
@client.encryption(:method => :simple_tls)
@client.auth(username, password)

以及我用来编码密码的方法

def encode_passwd(string)
            newstring = ""
            string = "\"" + string + "\""
            string.split("").each do |c|
                newstring = "#{newstring}#{c}\000"
            end
            return newstring
        end

希望这对将来的人有所帮助

4

2 回答 2

2

Net::LDAP::Password.generate不适用于 ActiveDirectory 。:unicodePwdLDAP-Entry-Attribute(说 ruby​​-gem用语net-ldap),您必须像这样对其进行编码

unicodepwd = "\"#{plain_text_password}\"".encode(Encoding::UTF_16LE).force_encoding(Encoding::ASCII_8BIT)

在此处查看有关编码的详细信息:https ://msdn.microsoft.com/en-us/library/cc223248.aspx

于 2015-08-25T08:46:04.593 回答
0

我刚刚发现 Net::LDAP 中已经包含了密码生成功能!

Net::LDAP::Password.generate(:md5, 'yourPlaintextPass')

文档在这里

于 2015-02-17T21:24:54.283 回答