C# 調用LDAP接口獲取域用戶信息

 

C# 調用LDAP接口獲取域用戶信息:web

 根據用戶顯示名稱和郵箱的前半部分,拉去相應的用戶列表,進行智能提示。 web 的提示控件能夠用select2.ui

 

 

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;
        //}
    }
}
相關文章
相關標籤/搜索