簡體   English   中英

如何從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/zh-CN/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

我想我已經知道了。 但我正在檢查以確保。

我從互聯網搜索中了解到,AD中有一個特殊的地方存儲域及其屬性CN=Partitions,CN=Configuration,DC=domain,DC=com

我向CN=SOUTHEAST,CN=Partitions,CN=Configuration,DC=domain,DC=com但是它始終缺少我需要的ldap對象屬性,即ncname ,即域的DN

如果您看到此答案,則說明出現問題的原因是我正在查詢全局目錄! 當查詢全局目錄時,您將缺少某些屬性。

因此,在執行多域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

這似乎有效。 我缺少的任何內容,請在下面評論或添加您自己的更好答案。 謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM