代码之家  ›  专栏  ›  技术社区  ›  Nicholas DiPiazza

如何从Java查询LDAP以从Active Directory的“netbiosDomain\samAccountName”获取对象的DN

  •  0
  • Nicholas DiPiazza  · 技术社区  · 6 年前

    netbiosDomain\samAccountName distinguishedName .

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

    有两个不同的用户:

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

    鉴于 东北\NICKD 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跨平台调用的东西。

    下面是可以转换的vbscript northeast\nickd 进入之内 CN=nickd,CN=Users,DC=northeast,DC=domain,DC=com :

    ' 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
    

    $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

    1 回复  |  直到 6 年前
        1
  •  0
  •   Nicholas DiPiazza    6 年前

    我想我已经弄明白了。但我正在四处查看。

    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域,请确保连接到父LDAP服务器并使用本地LDAP端口(默认情况下为端口389)。

    对…的质询 ldap://parent-ldap-host:389

    • 基本DN: CN=东南,CN=分区,CN=配置,DC=域,DC=com
    • 搜索筛选器: (objectClass=*)
    • 搜索范围: wholeSubtree