【问题标题】:How to query LDAP from Java to get an object's DN from the "netbiosDomain\samAccountName" from Active Directory如何从 Java 查询 LDAP 以从 Active Directory 的“netbiosDomain\samAccountName”获取对象的 DN
【发布时间】:2018-09-20 20:31:56
【问题描述】:

我需要从 Java 查询 LDAP 以将用户或组的 netbiosDomain\samAccountName 转换为 distinguishedName

例如

有两个子域: * DC=northeast,DC=domain,DC=com * DC=southeast,DC=domain,DC=com

并且有 2 个不同的用户:

  • NORTHEAST\NICKD = CN=nickd,CN=Users,DC=northeast,DC=domain,DC=com
  • SOUTHEAST\NICKD = CN=nickd,CN=Users,DC=southeast,DC=domain,DC=com

给定NORTHEAST\NICKD,我如何查询 ldap 以将其转换为CN=nickd,CN=Users,DC=northeast,DC=domain,DC=com

基本上,这个问题可以重新问:如何在 LDAP 中查询 netbios 域的 distingushedName?

这里的答案https://social.technet.microsoft.com/Forums/scriptcenter/en-US/dbbeeefd-001b-4d1d-93cb-b44b0d5ba155/how-do-you-search-for-a-domain-samaccountname-in-active-directory?forum=winserverDS&prof=required提供了可以做到的vbscript和powershell命令。但我需要一个可以做到的 LDAP 查询。或者任何可以从 Java 以跨平台方式调用的东西。

这里是可以将northeast\nickd转换成CN=nickd,CN=Users,DC=northeast,DC=domain,DC=com的vbscript:

' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

' Specify the NetBIOS name of the domain.
strNetBIOSDomain = "northeast"

' Specify the NT name of the user.
strNTName = "nickd"

' Use the NameTranslate object to convert the NT user name to the
' Distinguished Name required for the LDAP provider.
Set objTrans = CreateObject("NameTranslate")
' Initialize NameTranslate by locating the Global Catalog.
objTrans.Init ADS_NAME_INITTYPE_GC, ""
' Use the Set method to specify the NT format of the object name.
objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & "\" & strNTName
' Use the Get method to retrieve the RFC 1779 Distinguished Name.
strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)

' Escape any "/" characters with backslash escape character.
' All other characters that need to be escaped will be escaped.
strUserDN = Replace(strUserDN, "/", "\/")

Wscript.Echo strUserDN

还有powershell:

$Name = "northeast"
$Domain = "nickd"

# Use the NameTranslate object.
$objTrans = New-Object -comObject "NameTranslate"
$objNT = $objTrans.GetType()

# Initialize NameTranslate by locating the Global Catalog.
$objNT.InvokeMember("Init", "InvokeMethod", $Null, $objTrans, (3, $Null))
# Specify NT name of the object.
# Trap error if object does not exist.
Try
{
    $objNT.InvokeMember("Set", "InvokeMethod", $Null, $objTrans, (3, "$Domain\$Name"))
    # Retrieve Distinguished Name of the object.
    $DN = $objNT.InvokeMember("Get", "InvokeMethod", $Null, $objTrans, 1)

    $DN
}
Catch
{
    "Bad name: $Domain\$Name"
}

相关:https://serverfault.com/questions/234041/can-an-ldap-query-on-ad-provide-the-netbios-domain-name-for-a-single-account-whe

【问题讨论】:

    标签: java active-directory ldap


    【解决方案1】:

    我想我已经明白了。但我正在四处检查以确保。

    我从互联网搜索中了解到,AD 中有一个特殊的位置存储域及其属性CN=Partitions,CN=Configuration,DC=domain,DC=com

    我正在查询CN=SOUTHEAST,CN=Partitions,CN=Configuration,DC=domain,DC=com,但它总是缺少我需要的 ldap 对象属性,即ncname,它是域的DN

    如果您看到this answer,它表明我的问题的原因是我正在查询全局目录!当您查询全局目录时,您会丢失某些属性。

    因此,在对用户和组进行多域 LDAP 搜索时,您确实需要使用全局目录(默认为 3268 端口),否则您将无法从子域中获取用户/组。但在执行 LDAP 查询以获取 netbios 域的 DN 时,请确保连接到父 LDAP 服务器并使用本地 ldap 端口(默认为端口 389)。

    ldap://parent-ldap-host:389 的查询变为:

    • 基础 DN:CN=SOUTHEAST,CN=Partitions,CN=Configuration,DC=domain,DC=com
    • 搜索过滤器:(objectClass=*)
    • 搜索范围:wholeSubtree
    • 属性:ncname

    这似乎有效。我缺少的任何内容请在下面发表评论或添加您自己更好的答案。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多