如何从 Java 查询 LDAP 以从 Active Directory "netbiosDomain\samAccountName" 获取对象的 DN
How to query LDAP from Java to get an object's DN from the "netbiosDomain\samAccountName" from Active Directory
我需要从 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"
}
我想我已经弄明白了。但我正在四处查看以确保。
我从网上搜索得知,在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),否则您将无法从子域中获取 users/groups。但是当执行 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
这似乎有效。我遗漏的任何内容请在下面评论或添加您自己的更好答案。谢谢。
我需要从 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"
}
我想我已经弄明白了。但我正在四处查看以确保。
我从网上搜索得知,在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),否则您将无法从子域中获取 users/groups。但是当执行 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
这似乎有效。我遗漏的任何内容请在下面评论或添加您自己的更好答案。谢谢。