使用openldap c#从活动目录中获取用户

时间:2015-11-30 14:04:59

标签: c# ldap openldap

我是一个使用c#的新手,我需要创建一个扩展程序,转到目录并检索有关用户的信息,如用户名,姓名和电子邮件。

这是我创建的登录验证方法,它正在运行。

public void MssValidateUserLDAP(string ssHostname, string ssBaseDN, string ssUsername, string ssPassword, out bool ssOk, out string ssErrorMessage) {
            ssOk = false;
            ssErrorMessage = string.Empty;
            String ssBaseRDN = string.Empty; // stores user RDN for authentication

            LdapConnection connection = new LdapConnection(ssHostname);

            connection.AuthType = AuthType.Basic;

            try
            {
                SearchRequest searchRequest = new SearchRequest();

                // Search parameters
                searchRequest.Scope = System.DirectoryServices.Protocols.SearchScope.OneLevel;
                searchRequest.DistinguishedName = ssBaseDN;
                searchRequest.Filter = ssUsername;


                // cast the returned directory response as a SearchResponse object
                SearchResponse searchResponse = (SearchResponse)connection.SendRequest(searchRequest);


                // enumerate the entries in the search response
                foreach (SearchResultEntry entry in searchResponse.Entries)
                {
                    ssBaseRDN = entry.DistinguishedName;
                    ssOk = true;
                }
                if (ssBaseRDN != "")
                {
                    connection.Bind(new NetworkCredential(ssBaseRDN, ssPassword));
                }
                else { ssOk = false; ssErrorMessage = "User not found"; }
            }
            catch (Exception e)
            {
                ssErrorMessage = e.GetType().Name + " " + e.Message;

                ssOk = false;
            }
        }

这是我能够做到的,但它无法正常工作,我无法找到背后的原因。任何帮助将不胜感激!

public void MssSearch(string ssUsername, string ssPassword, string ssPath, out RLUserRecordList ssUsers, out string ssErrorMessage) {

                 ssErrorMessage = "";
                 ssUsers = new RLUserRecordList(null);

                 try
            {                 
            RLUserRecordList aux = new RLUserRecordList();
            DirectoryEntry rootEntry = new DirectoryEntry(ssPath,ssUsername,ssPassword);
            DirectorySearcher searcher = new DirectorySearcher(rootEntry);
            foreach(SearchResult result in searcher.FindAll()) 
            {
                RCUserRecord u = new RCUserRecord(Convert.ToString(result.Properties["cn"][0]));
                aux.Append(u);
            }
            ssUsers = aux;

            }
            catch (Exception e){
                ssErrorMessage = e.GetType().Name + " " + e.Message;
            }


        } // MssSearch

0 个答案:

没有答案