C#连接到AD并搜索636 SSL

时间:2015-11-12 15:41:56

标签: c# ssl active-directory

我对C sharp很新。我想要做的事情很容易在powershell中完成,但我想在C sharp中做到这一点。我有一个项目的一部分,我正在做什么,我必须以某种方式查询Active Directory并获得所有用户。我有两个要拿取的属性: 1. objectGUID 2. msDS-UserPasswordExpiryTimeComputed

然后我必须将此信息插入到包含两列的简单SQL表中。我实际上没有得到SQL表部分。现在,我纯粹是在一个基于C#控制台的应用程序上工作,该应用程序从域中获取帐户。

但是,我的公司已经指示我只能通过端口636连接到AD。在我测试这个时,从38上获取这些数据很容易。我已经附加了代码,我一直在研究。但是当我添加以下行时,我无法弄清楚为什么它不起作用:

de.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;

当我包含上述行时,我在下面的行中收到错误消息:

SearchResultCollection src = ds.FindAll();

错误是:

  

{“发生了操作错误。\ r \ n”}

这是我正在使用的代码。我也不想在此代码中硬编码用户/密码。这是因为它将由调度程序服务器作为作业调用。作业将在某个安全上下文下运行,该上下文将具有对AD域的读取权限。

/////// start of code ///////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Security.Principal;

namespace DirectorySearcherTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string adsPath = "LDAP://OU=DisabledAccts,OU=Secondary Accounts,OU=Access Management,DC=dev,DC=net";

            // Create a list of attributes that you want to query from AD.
            List<string> attributes = new List<string>();
            attributes.Add("objectGUID");
            attributes.Add("msDS-UserPasswordExpiryTimeComputed");
            //----------------------------------------------------------------

            DirectoryEntry de = new DirectoryEntry(adsPath);
            de.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;
            DirectorySearcher ds = new DirectorySearcher(de);
            ds.Filter = "(&(objectCategory=Person)(objectClass=User))";

            foreach (string attribute in attributes)
            {
                ds.PropertiesToLoad.Add(attribute);
            }


            SearchResultCollection src = ds.FindAll();

            foreach (SearchResult sr in src)
            {
                ResultPropertyCollection fields = sr.Properties;

                foreach (string ldapfield in fields.PropertyNames)
                {
                    foreach (Object myCollections in fields[ldapfield])
                    {
                        switch (ldapfield.ToLower())
                        {

                            case "objectguid":
                                byte[] myguid = (byte[])myCollections;
                                Guid guid = new Guid(myguid);
                                Console.WriteLine("{0}\t{1}", ldapfield, guid.ToString());
                                break;

                            case "msds-userpasswordexpirytimecomputed":
                                Console.WriteLine("{0}\t{1}", ldapfield, myCollections.ToString());
                                break;

                            default:
                                Console.WriteLine("{0}\t{1}", ldapfield, myCollections.ToString());
                                break;
                        }

                    }
                }
            }


            Console.ReadKey();

        }
    }
}

/////// End of code ///////////////

0 个答案:

没有答案