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. 概率生成函数(PGF)简记

    基本搬运自<浅谈生成函数在掷骰子问题上的应用>. 对于定义在非负整数上的离散随机变量 \(X\),级数 \(F(z) = \sum\limits_{i\ge 0} \operatornam ...

  2. 464. 我能赢吗 (Medium)

    问题描述 464. 我能赢吗 (Medium) 在 "100 game" 这个游戏中,两名玩家轮流选择从 1 到 10 的任意整数,累计整数和,先使得累计整数和 达到或超过 100 ...

  3. linux中磁盘如何由dos格式怎么变为gpt格式

    一般情况下,我们进行磁盘分区管理使用gdisk命令比较方便快捷,但假如我们想要大于2T的磁盘使用fdisk命令已经无法使用,此刻我们该怎么办?这时我们可以使用parted命令来把磁盘转换为gpt格式, ...

  4. UE4启动顺序

    GameMode PlayerController Actor Level gameMode , playerController控制pawn , 激活默认相机active camera , getP ...

  5. SI24R2H_2.4GHz超低功耗无线发射与125KHZ接收一体芯片

    Si24R2H是一颗工作在2.4GHz ISM频段发射和125KHZ接收,专为超低功耗无线应用场景设计,集成嵌入式基带的无线收发单芯片.发射工作频率范围为2400MHz-2525MHz,共有125个1 ...

  6. ElasticSearch (Es) 分组查询 记录

    首先表对应的实体类型: public class bm_info{ /// <summary> /// 单位 /// </summary> public  string sou ...

  7. Metasploit渗透测试框架一

    Metasploit简介 Metasploit是一个渗透测试平台,使您能够查找,利用和验证漏洞.该平台还有Metasploit Pro. Metasploit是一个免费的.可下载的框架,本身自带数百已 ...

  8. SQL 错误 [1105] [HY000]: errCode = 2, detailMessage = select list expression not produced by aggregation output (missing from GROUP BY clause?): ......

    SQL 错误 [1105] [HY000]: errCode = 2, detailMessage = select list expression not produced by aggregati ...

  9. spring mvc问题:源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示

    HTTP状态 404 - 未找到 类型 状态报告 描述 源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源表示. 我的问题:spring mvc无法访问到映射的controller,页面显 ...

  10. desginer启动就直接卡死

    博主经验: 请不要开有道词典    请不要开有道词典         请不要开有道词典