C# 调用LDAP接口获取域用户信息
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接口获取域用户信息的更多相关文章
- 小白学react之网页获取微信用户信息
通过上一篇<小白学react之EJS模版实战>我们学习了怎样通过EJS模版生成我们高定制化的index.html文件. 本篇我们将会继续延续我们的alt-tutorial项目的实战计划.去 ...
- SpringBoot中获取微信用户信息从未如此简单!
前言 不知道你是否参加过拼多多上邀请微信好友砍价功能,这个功能实现首先需要考虑的就是获取微信用户的信息.获取用户信息就是获取公众号下微信用户的信息,今天我就来讲讲如何从公众号下获取微信用户信息. 需要 ...
- java、JavaScript获取微信用户信息登录优化方案
1.获取微信用户信息要调用微信的好几个接口,再加上自己系统的接口就会变的很慢,影响用户体验,之前走过的弯路我就不赘述了,直接说新的方案. 2.第一步都是向微信发起获取用户code请求: 请求接口:ht ...
- java接口对接——别人调用我们接口获取数据
java接口对接——别人调用我们接口获取数据,我们需要在我们系统中开发几个接口,给对方接口规范文档,包括访问我们的接口地址,以及入参名称和格式,还有我们的返回的状态的情况, 接口代码: package ...
- QQ登入(6)腾讯微博-获取微博用户信息,发送微博
1.1获取weibo用户信息 //先登入授权,可以参考QQ登入(1) Weibo mWeibo = new Weibo(this, mQQAuth.getQQToken()); mWeibo.getW ...
- Magicodes.WeiChat——使用OAuth 2.0获取微信用户信息
使用Magicodes.WeiChat,可以很方便的获取到微信用户的信息.在使用OAuth 2.0之前,你先需要做以下操作: 1)在开发者中心修改[网页授权获取用户基本信息],在弹出的界面输入自己的根 ...
- Delphi调用API函数获取Windows目录信息、获取System目录信息、获取Temp临时文件目录信息
var Str1, Str2: Array[..Max_Path]of Char;//开辟缓冲区 Str3: Array[..]of Char; begin GetWindowsDirectory(@ ...
- python flask获取微信用户信息报404,nginx问题
在学习flask与微信公众号时问题,发现测试自动回复/wechat8008时正常,而测试获取微信用户信息/wechat8008/index时出现404.查询资料后收发是nginx配置问题. 在loca ...
- android调用webservice接口获取信息
我的有一篇博客上讲了如何基于CXF搭建webservice,service层的接口会被部署到tomcat上,这一篇我就讲一下如何在安卓中调用这些接口传递参数. 1.在lib中放入ksoap2的jar包 ...
- 玩玩微信公众号Java版之五:获取关注用户信息
在关注者与公众号产生消息交互后,公众号可获得关注者的OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的.对于不同公众号,同一用户的openid不同).公众号可通过本接口来根据Op ...
随机推荐
- 【剑指Offer】【树】序列化二叉树
题目:请实现两个函数,分别用来序列化和反序列化二叉树 二叉树的序列化是指:把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串,从而使得内存中建立起来的二叉树可以持久保存.序列化可以基于先序. ...
- PTA---求月天数
最近做了几次模拟考试,对于求月天数这个题目有了更深一点的理解. 这个题的题目基本就是让用户输入年份和月份,给出该月有多少天. 对于这个题,首先就要考虑年份的问题,因为闰年和非闰年在二月是有一点不同的, ...
- day01-java流程
Scanner对象 基本语法: Scanner s = new Scanner(System.in); next()方法 nextLine()方法 顺序结构 选择结构 if单选结构 语法: if(布尔 ...
- C++ 手动实现栈(stack) (课后作业版)
栈,一种遵循先进先出原则的数据结构,可以用顺序表实现,也可以用链表进行实现. 这里我使用数组实现方法,包含了进栈,出栈,访问栈顶等功能,以及一些辅助功能. 栈Stack类定义如下: template ...
- 重启nginx 报错 nginx.pid
先检查 nginx -c /etc/nginx/nginx.conf 看看端口是否被占用 若占用杀掉 ps -ef | grep nginx kill --- 然后再用nginx -c /etc/ng ...
- 微积分 I 笔记
1.1 集合 这一节复习了高中关于集合的基础知识 介绍了一些新的概念 笛卡尔积 (Cartesian Product) 集合 \(X\) 与 \(Y\) 的笛卡尔积 (直积) \(X \times Y ...
- Git探究
Git Git是什么? Git是一个开源的分布式版本控制系统. 1.什么是版本控制? 字面意思,控制版本,存储.追踪目录和文件的修改历史.市面上有各式各样的版本控制工具,比较常用的SVN.Git等等. ...
- HIVE- set
SET hive.execution.engine = tez;SET hive.tez.auto.reducer.parallelism = true;SET hive.auto.convert.j ...
- pve apt 更新
解决pve6.2/pve6.3 安装ifupdown2,失败问题 https://www.cnblogs.com/lovesKey/p/14400874.html https://blog.whsir ...
- python菜鸟学习: 14. GUI界面化使用
# 获取输入框中的内容 def getVars(): global outterDomain1, innertDomian1, guestEid1, appName1, unicodeName1, r ...