C#获取LDAP属性语法OID

时间:2014-09-25 14:39:18

标签: c# ldap

我需要检查LDAP属性的语法OID,但我找不到任何好的起点。我正在使用C#和当前的System.DirectoryServices.Protocols(必须保持通用/非Active Directory特定)。

例如,使用Apache Directory Studio我们可以看到,在Active Directory中,“distinguishedName”属性的语法为OID“1.3.6.1.4.1.1466.115.121.1.12”。

有人可以请我朝正确的方向踢我吗?

1 个答案:

答案 0 :(得分:1)

好的,我明白了。我使用了thisthis SO帖子的组合来解决这个问题。在这里缝合在一起,如果有任何其他需要的话。请注意,这适用于Active Directory和OpenLDAP(使用System.DirectoryServices.Protocols)。

var ldapConnection = new LdapConnection( "hostname.tld" );
ldapConnection.AuthType = AuthType.Yours;
ldapConnection.Credential = new NetworkCredential( "username", "password", "domain" );
ldapConnection.SessionOptions.ProtocolVersion = 3;

// Find the subschema first...
var searchRequest = new SearchRequest( null, "(objectClass=*)", SearchScope.Base, "subschemasubentry" );
var searchResponse = (SearchResponse) ldapConnection.SendRequest( searchRequest );

var subSchemaArray = searchResponse.Entries[0].Attributes["subschemasubentry"].GetValues( typeof( String ) );
var subSchema = (String) subSchemaArray[0];

// Now query the LDAP server and get the attribute types
searchRequest = new SearchRequest( subSchema, "(objectClass=*)", SearchScope.Base, "attributetypes" );
searchResponse = (SearchResponse) ldapConnection.SendRequest( searchRequest );

foreach ( string attributeType in searchResponse.Entries[0].Attributes["attributeTypes"].GetValues( typeof( String ) ) )
{
    // This is a chunky string, but the name and syntax OID is listed here
    Console.WriteLine(attributeType);
}