C# 调用LDAP接口获取域用户信息:

根据用户显示名称和邮箱的前半部分,拉去相应的用户列表,进行智能提示。 web 的提示控件可以用select2.

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Diagnostics;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.Protocols;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.DirectoryServices.Protocols; namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{ /*https://support.jumpcloud.com/customer/portal/articles/2439978-filtering-by-user-or-group-in-ldap-search-filters-
* Get all entries: (objectClass=*)
* Get entries containing "bob" somewhere in the common name:(cn=*bob*)
* Get entries with a common name greater than or equal to "bob":(cn>='bob')
* Get all user entries with an email attribute and a surname equal to "smith":(&(sn=smith)(objectClass=user)(email=*))
* Get all user entries with a common name that starts with "andy", "steve", or "margaret":(&(objectClass=user)(| (cn=andy*)(cn=steve*)(cn=margaret*)))
*/
//first
Stopwatch sw = new Stopwatch();
sw.Start();
//LDAPS的url也要用LDAP://
DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://aa.bb.global.baidu.com:3269", "username", "password",
AuthenticationTypes.SecureSocketsLayer);
//directoryEntry.Options
DirectorySearcher searcher = new DirectorySearcher(directoryEntry)
{
PageSize = 3,
SizeLimit = 3,
Asynchronous = false,
CacheResults = false,
ClientTimeout = new TimeSpan(0, 0, 1),
Filter = "(&(objectCategory=person)(objectClass=user)(|(cn=lm*)(mail=lm*)))"
}; searcher.PropertiesToLoad.AddRange(new[] { "cn", "sn", "displayName", "mail", "department", "company" }); SearchResultCollection result = searcher.FindAll();
sw.Stop(); foreach (SearchResult oResult in result)
{
Console.WriteLine(oResult.Properties["cn"][0] + " : " + (!oResult.Properties.Contains("mail") ? "" : oResult.Properties["mail"][0])
+ " : " + (!oResult.Properties.Contains("department") ? "" : oResult.Properties["department"][0])
+ "-" + (!oResult.Properties.Contains("company") ? "" : oResult.Properties["company"][0]));
} Console.WriteLine(sw.ElapsedMilliseconds);
Console.ReadKey();
} //second //LdapConnection ldapConnection = new LdapConnection("cn1.global.ctrip.com:636"); //var networkCredential = new NetworkCredential("FeebackEmailCheck", "O&xbu{c]=C");
//ldapConnection.SessionOptions.SecureSocketLayer = true;
//ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };
//ldapConnection.AuthType = AuthType.Basic;
//ldapConnection.Bind(networkCredential); //SearchRequest request = new SearchRequest("DC=xxx,DC=xxx,DC=xxx", "(sAMAccountName=3074861)", System.DirectoryServices.Protocols.SearchScope.Subtree);
//SearchResponse response = (SearchResponse)ldapConnection.SendRequest(request); //if (response.Entries.Count == 1)
//{
// SearchResultEntry entry = response.Entries[0];
// string DN = entry.DistinguishedName;
//} //3rd:
//LdapConnection conn = new LdapConnection("cn1.global.ctrip.com:636");
//var op = conn.SessionOptions;
//op.ProtocolVersion = 3;
//op.SecureSocketLayer = true;
//op.VerifyServerCertificate += delegate { return true; }; //conn.AuthType = AuthType.Basic;
//var cred = new NetworkCredential("FeebackEmailCheck", "O&xbu{c]=C"); ////conn.Credential = cred;
//try
//{
// conn.Bind(cred); // if (op.SecureSocketLayer)
// {
// Console.WriteLine("SSL for encryption is enabled - SSL information:");
// }
//}
//catch (Exception ex)
//{ // throw;
//}
}
}

  

C# 调用LDAP接口获取域用户信息的更多相关文章

  1. 小白学react之网页获取微信用户信息

    通过上一篇<小白学react之EJS模版实战>我们学习了怎样通过EJS模版生成我们高定制化的index.html文件. 本篇我们将会继续延续我们的alt-tutorial项目的实战计划.去 ...

  2. SpringBoot中获取微信用户信息从未如此简单!

    前言 不知道你是否参加过拼多多上邀请微信好友砍价功能,这个功能实现首先需要考虑的就是获取微信用户的信息.获取用户信息就是获取公众号下微信用户的信息,今天我就来讲讲如何从公众号下获取微信用户信息. 需要 ...

  3. java、JavaScript获取微信用户信息登录优化方案

    1.获取微信用户信息要调用微信的好几个接口,再加上自己系统的接口就会变的很慢,影响用户体验,之前走过的弯路我就不赘述了,直接说新的方案. 2.第一步都是向微信发起获取用户code请求: 请求接口:ht ...

  4. java接口对接——别人调用我们接口获取数据

    java接口对接——别人调用我们接口获取数据,我们需要在我们系统中开发几个接口,给对方接口规范文档,包括访问我们的接口地址,以及入参名称和格式,还有我们的返回的状态的情况, 接口代码: package ...

  5. QQ登入(6)腾讯微博-获取微博用户信息,发送微博

    1.1获取weibo用户信息 //先登入授权,可以参考QQ登入(1) Weibo mWeibo = new Weibo(this, mQQAuth.getQQToken()); mWeibo.getW ...

  6. Magicodes.WeiChat——使用OAuth 2.0获取微信用户信息

    使用Magicodes.WeiChat,可以很方便的获取到微信用户的信息.在使用OAuth 2.0之前,你先需要做以下操作: 1)在开发者中心修改[网页授权获取用户基本信息],在弹出的界面输入自己的根 ...

  7. Delphi调用API函数获取Windows目录信息、获取System目录信息、获取Temp临时文件目录信息

    var Str1, Str2: Array[..Max_Path]of Char;//开辟缓冲区 Str3: Array[..]of Char; begin GetWindowsDirectory(@ ...

  8. python flask获取微信用户信息报404,nginx问题

    在学习flask与微信公众号时问题,发现测试自动回复/wechat8008时正常,而测试获取微信用户信息/wechat8008/index时出现404.查询资料后收发是nginx配置问题. 在loca ...

  9. android调用webservice接口获取信息

    我的有一篇博客上讲了如何基于CXF搭建webservice,service层的接口会被部署到tomcat上,这一篇我就讲一下如何在安卓中调用这些接口传递参数. 1.在lib中放入ksoap2的jar包 ...

  10. 玩玩微信公众号Java版之五:获取关注用户信息

    在关注者与公众号产生消息交互后,公众号可获得关注者的OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的.对于不同公众号,同一用户的openid不同).公众号可通过本接口来根据Op ...

随机推荐

  1. Rancher 使用介绍(可以通过界面管理 K8s 平台)

    参考链接 https://blog.csdn.net/weixin_46902396/article/details/122433622 https://www.hugedomains.com/dom ...

  2. 一台服务器部署ShareWAF,后面接多台Web服务器,该如何配置?

    ShareWAF做为WAF,可以不只是WAF,还可以充当负载或路由的角色. 比如可以有这样一种部署架构: 在此结构中,ShareWAF部署于一台服务器,后面接多台独立的WEB服务器. ShareWAF ...

  3. 使用iperf测试网卡性能

    1.目标 测试网卡通信性能,同时可以通过改变连接方式(从两台PC网线直连,切换到通过交换机连接)测试交换机最高速率性能. 2.使用工具 硬件:两台PC机(本例用win10 64位).数根网线.交换机 ...

  4. 查电脑并修改IP地址,你晓得吗?

    查电脑并修改IP地址,你晓得吗?   好记性不如烂笔头,古人的话,浅显却好有深意,越品越有味道.   每次都会忘记怎么查电脑IP,那么今天就写下来吧! 方法一:通过命令行查询IP地址   快捷键Win ...

  5. docker 安装 服务

    服务所需 mysql,redis,nfs,rsync,nginx,以及自己的后端服务 mysql docker run --restart=always -d -v /etc/localtime:/e ...

  6. 前端实现电子签名(web、移动端)通用组件(canvas实现)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. PHP Array数组

    PHP中的数组实际上是一个有序映射.映射是一种把values关联到keys的类型.此类型在很多方面做了优化,因此可以把它当成真正的数组,或列表(向量),散列表(是映射的一种实现),字典,集合,栈,队列 ...

  8. Mysql之迂回连接术

    转载请注明来源:https://www.cnblogs.com/Sherlock-L/p/14932870.html 关键词:OmniDB.Mysql Router 背景:项目的测试数据库放在了生产机 ...

  9. 搭建 springboot 应用

    一.采用 Spring Initializr 搭建springboot应用 步骤: 1.只需要打开网址: https://start.spring.io/: 2.选择使用 Maven 构建,语言 ja ...

  10. Windows下的挖矿木马查杀

    MS016小组(原创) 上一篇文章 简单讲了一下挖矿木马 大概流程  文章地址: https://www.cnblogs.com/ms016/articles/7978880.html 今天讲分析一个 ...