【发布时间】: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"
}
【问题讨论】:
标签: java active-directory ldap