使用LDAP接口获取AD域用户
2013-04-09 23:37
716 查看
下面代码是从<Windows 2000 Active Directory程序设计> 书上敲下来的, 做了一些修改改, 代码现在能通过编译,
但是里面有些不足之处, 如很多地方没有判断是否处理成功(代码中// TODO: 处), 这样会导致程序挂掉.
等我有时间调试好程序后再传上来. 之后我会把书上相关的代码尽量多敲一些下来方便大家.
但是里面有些不足之处, 如很多地方没有判断是否处理成功(代码中// TODO: 处), 这样会导致程序挂掉.
等我有时间调试好程序后再传上来. 之后我会把书上相关的代码尽量多敲一些下来方便大家.
/************************************************************************/ /* Windows2000ActiveDirectory程序设计.pdf P36 */ /* LDAPEnumTop.c */ /* 作用: 连接到AD域, 并查找顶层对象, 可以通过递归此函数列出所有对象*/ /************************************************************************/ #include <Windows.h> #include <stdio.h> #include <Winldap.h> #pragma comment(lib,"wldap32.lib") // 书上没有此项, 增加此项才能通过编译 int main(int argc, char **argv) { PLDAP pldapSession; // LDAP session data PLDAPMessage plmsgSearchResponse; // server allocated response to search request PLDAPMessage plmsgEntry; // server allocated response to entry request PCHAR pszDN; // LDAP distinguished name stringn PCHAR* ppszDomainDN = NULL; // Domain DN(string allocated by LDAP library) // start an LDAP session to nearest LDAP server // 不用host 的方法只有Win2000上才能用, 2003 上不能使用 pldapSession = ldap_init(NULL, LDAP_PORT); // ldap_init(hostname, port) // TODO: 判断成功与否, 当初始化失败时不应继续 // authenticate using user's current credentials ldap_bind_s(pldapSession, NULL, NULL, LDAP_AUTH_NEGOTIATE); // serarch the root of the LDAP server ldap_search_s(pldapSession, // session handle NULL, // locaation 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 results // TODO: 判断是否成功 // using the defaultNameingContext attribute, get the distinguished name of the domain ppszDomainDN = ldap_get_values(pldapSession, plmsgSearchResponse, "defaultNamingContest"); // TODO: 判断是否成功 // 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 *ppszDomainDN, // location in directory to start search LDAP_SCOPE_ONELEVEL, // search first level below the base entry NULL, // SEARCH FOR ALL objects NULL, // no attributes spectified, return all attributes FALSE, // return attributes types and values &plmsgSearchResponse); // server allocates and fills with search results // TODO: 判断是否成功 // get the first entry form the search results plmsgEntry = ldap_first_entry(pldapSession, plmsgSearchResponse); while(plmsgEntry){ // get the distinguished name of the entry pszDN = ldap_get_dn(pldapSession, plmsgEntry); // TODO: 判断是否成功 // print the DN of the entry printf("%s\n", pszDN); // get next entry plmsgEntry = ldap_next_entry(pldapSession, plmsgEntry); } // instruct the library toi free the search results ldap_value_free(ppszDomainDN); // close the session ldap_unbind(pldapSession); return 0; }
相关文章推荐
- Java使用LdAP获取AD域用户
- Java使用LdAP获取AD域用户
- Java使用LdAP获取AD域用户
- LDAP方式连接AD获取用户信息
- LDAP方式连接AD获取用户信息
- 使用新浪的接口获取用户当前IP的位置地点身份城市地区
- LDAP方式连接AD获取用户信息
- 微信商城中使用微信支付接口获取用户地址
- 小程序使用wx.chooseAddress获取用户手机号码,微信chooseAddress接口获取收货信息
- js根据ip地址获取用户地点的接口使用方法
- 使用微信公众号接口一键获取全部关注用户的信息(openid,nickname,city,country,groupid,targetlist)
- C# 调用LDAP接口获取域用户信息
- 获取某条新浪微博的所有转发用户列表(使用repost_timeline接口)
- C#通过IP获取用户地理位置省份城市的接口使用
- LDAP方式连接AD获取用户信息
- VB.Net中使用LDAP验证基于微软AD的登录用户
- LDAP方式连接AD获取用户信息
- 使用组策略获取AD中所有计算机当前登录用户信息
- 求助:php通过新浪微博接口 api 如果获取某一地区下所有用户的微博列表?或使用包含某一关键词的微博列表