C# 调用LDAP接口获取域用户信息
2016-08-05 17:37
513 查看
C# 调用LDAP接口获取域用户信息:
根据用户显示名称和邮箱的前半部分,拉去相应的用户列表,进行智能提示。 web 的提示控件可以用select2.
根据用户显示名称和邮箱的前半部分,拉去相应的用户列表,进行智能提示。 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#的编程小白创建计算器全过程并恶意卖萌(2之增加卖萌元素)
- C#开发学习笔记:向Dev控件库中的GridControl添加自定义复选框
- VS2015 C#.net4.6 windows的定时服务
- C#实现的三种方式实现模拟键盘按键
- C# winform间窗体传值简单Demo
- C#如何判断是哪个类的那个方法调用了当前方法
- AE用线来分割线面(C#2010+AE10.0… .
- C# 如何调用WebServer函数
- C# AD 验证登陆
- .NET 调用方信息的获取
- C#中定时器的用法
- C#读取写入excel单元格
- C#入门6.5——字符串的格式化
- C# 3DES加密 解密
- 如何使用C#Windows窗体完成文件浏览功能:
- C#如何判断是web程序还是winform
- C#入门6.4——字符串的比较
- C#新手入门代码 LINQ TO XML xattribute的用法示例
- 单例设计模式(C#) 懒汉模式
- 七牛-c#断点上传