ADP-ACTIVE DIRECTORY Programming-02

xiaoxiao2021-03-06  14

ADP20050312 :: LDAP API directly

ADP20050312 :: LDAP API directly

ASDI is based on COM, we will say later. Let's take a look at the more direct LDAP API.

Lightweight Directory Access Protocol is described in detail in the RFC below:

l RFC 2251 Lightweight Directory Access Protocol (V3)

l RFC 2252 Lightweight Directory Access Protocol (V3): Attribute Syntax Definitions

l RFC 2253 Lightweight Directory Access Protocol (V3): UTF-8 String Repesentation of Distinguished Names

l RFC 2254 THE STRING REPRESENTATION OF LDAP SEARCH FILTERS

l RFC 2255 THE LDAP URL FORMAT

l RFC 2256 A Summary of the X.500 (96) User Schema for Use with LDAPV3

l RFC 2829 Authentication Methods for LDAP

l RFC 2696 LDAP Control Extension for Simple Paged RESULTS MANIPULATION

l RFC 1487 X.500 Lightweight Directory Access Protocol (Covers Version 1, Now Obsolete)

l RFC 1777 X.500 Lightweight Directory Access Protocol (Covers LDAPV2)

l RFC 1798 Connection-Less Lightweight X.500 Directory Access Protocol

l RFC 1823 THE LDAP Application Program Interface

l RFC 2247 Using Domains in LDAP / X.500 DISTINGUISHED NAMES

l RFC 2377 Naming Plan for Internet Directory-enabled Applications

The RFC 1823 defines the APIs that can be used in C language.

You can access the address below to familiarize yourself with LDAP

http://www.openldap.org/

http://computer.mblogger.cn/mwg_arden/posts/29816.aspx

It is best to watch RFC

We give a C program LDAPENUMTOP.C this program is "Windows 2000 Active Directory Programming" in Charles Oppermann:

#include

#include

#include

void main ()

{

PLDAP PLDAPSession; // LDAP Session Data

PldapMessage PlmsgSearchResponse; // Server Allocated Response To

// Search Request

PLDAPMESSAGE PLMSGENTRY; // Server Allocated response to entry requestpchar pszdn; // ldap distinguished name string

Pchar * ppszdomaindn = null; // Domain DN (String Allocated by LDAP

// library

// Start An LDAP Session to Nearest LDAP Server

PLDAPSession = ldap_init (null, ldap_port);

// Authenticate Using User's Current Credentials

LDAP_BIND_S (PLDAPSESSION, NULL, NULL, LDAP_AUTH_NEGOTIATE);

// Search the root of the ldap server

LDAP_SEARCH_S (PLDAPSession, // Session Handle

Null, // location to Start Search, Null Specifies TOP

// Level

LDAP_SCOPE_BASE, // Search Only the root entry (rootdse)

Null, // Search for All Objects (ONLY One for the

// rootdse)

Null, // no attributes specified, return all attributes

False, // Return Attributes Types and Values

& plmsgSearchResponse); // Server Allocates and Fills

// with search result

// using the defaultnamingcontext attribute, get the distinguished

// Name of the domain

PpszdomainDn = ldap_get_values ​​(PLDAPSession, PLMSGSEARCHRESPONSE,

"defaultnamingContext");

// Display Info

Printf ("Listing Objects at% s. / npress ctrl c to interrupt./n",

* PPSZDomainDN;

// Search First Level of Root Container

LDAP_SEARCH_S (PLDAPSession, // Session Handle

* ppszdomain, // location in Directory to Start Search

LDAP_SCOPE_ONELEVEL, // Search First Level Below The

// Base Entry

Null, // Search for All Objects

Null, // no attributes specified, return all attributes

False, // Return Attributes Types and Values

& plmsgSearchResponse); // Server Allocates and Fills

// with search result

// Get The First Entry from the search result

PLMSGENTRY = LDAP_FIRST_ENTRY (PLDAPSession, PLMSGSEARCHRESPONSE); While (PLMsgentry) {

// Get the distinguished name of the entry

Pszdn = ldap_get_dn (PLDAPSession, PLMsgentry);

//Print the dn of the entry

Printf ("% S / N", PSZDN);

// Get Next Entry

PLMSGENTRY = LDAP_NEXT_ENTRY (PLDAPSession, PLMSGENTRY);

}

// instruct the library to free the search result

LDAP_MSGFree (PLMSGSEARCHRESPONSE);

// free string allocated by the LDAP API

LDAP_VALUE_FREE (PPSZDomain);

// Close the session

LDAP_UNBIND (PLDAPSession);

}

You need to reference the WLDAP32.LIB library when compiling this C program. If you are adding to the VC environment directly in the Link tab, you need to add compile options such as CL, you need to add compilation options: CL LDAPENUMTOP.C / LINK WLDAP32.LIB

The program operation results are shown in our test environment:

Recall that only 7-line statement and two lines are used by I / O display output VBS code to get the same effect, but don't disappoint C, you will find its advantage.

The structural details are not disclosed, such as LDAP Struct, LDAPMESSAGE STRUCT. Their structure in MSDN is not disclosed, the following article explains these structures:

http://computer.mblogger.cn/mwg_arden/posts/29882.aspx

Experience with LDAP:

Advantages: The LDAP API provides low level access to the quick and low demand for directory information. And it can work very well on the platform that meets the RFC1823.

Disadvantages: This non-objective API uses difficult to use in a non-C programming environment.

There is a detailed introduction to LDAP on MSDN. You can refer to:

Today, MSDN can't help you find a position.

转载请注明原文地址:https://www.9cbs.com/read-47122.html

New Post(0)