第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 AD 組中獲取用戶(hù)的電子郵件

在 AD 組中獲取用戶(hù)的電子郵件

C#
梵蒂岡之花 2021-11-07 19:12:55
我正在使用此代碼獲取 AD 中特定組中的用戶(hù)列表private DirectoryEntry _directoryEntry = null;private DirectoryEntry SearchRoot{    get    {        if (_directoryEntry == null)        {            _directoryEntry = new DirectoryEntry(_ldapDomain, _user, PBKDF2Algorithm.Decrypt(_password, "PAssword"), AuthenticationTypes.Secure);        }        return _directoryEntry;    }}public List<User> GetUserFromGroup(String groupName){    List<User> userlist = new List<User>();    try    {        DirectorySearcher directorySearch = new DirectorySearcher(SearchRoot)        {            Filter = "(&(objectClass=group)(SAMAccountName=" + groupName + "))"        };        var results = directorySearch.FindOne();        if (results != null)        {            DirectoryEntry deGroup = new DirectoryEntry(results.Path, _user, PBKDF2Algorithm.Decrypt(_password, "PAssword"));            PropertyCollection pColl = deGroup.Properties;            int count = pColl["member"].Count;            for (int i = 0; i < count; i++)            {                string respath = results.Path;                string[] pathnavigate = respath.Split("CN".ToCharArray());                respath = pathnavigate[0];                string objpath = pColl["member"][i].ToString();                string path = respath + objpath;                DirectoryEntry user = new DirectoryEntry(path, _user, PBKDF2Algorithm.Decrypt(_password, "!twcActiveDirectory!"));                User userobj = User.GetUser(user);                userlist.Add(userobj);                user.Close();            }        }        return userlist.Where(item => !string.IsNullOrEmpty(item.FirstName) || !string.IsNullOrWhiteSpace(item.FirstName)).ToList();    }    catch (Exception ex)    {        return userlist;    }}返回的屬性不包含用戶(hù)的電子郵件地址,后來(lái)我找到了一種檢索用戶(hù)代理地址的方法,這正是我正在尋找的,但問(wèn)題是我只成功檢索了主根中的用戶(hù),而不是為一個(gè)特定的群體。那么,有什么方法可以合并兩個(gè)代碼?
查看完整描述

1 回答

?
慕村9548890

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊

我找到了一種方法,如下所示:

  1. 為每個(gè)組加載用戶(hù)

  2. 為每個(gè)用戶(hù)建立登錄名

  3. 使用 PrincipalContext 搜索每個(gè)用戶(hù)的登錄名

  4. 為每個(gè)用戶(hù)加載 ProxyAddresses 和 Mail 屬性

這是一個(gè)解釋這個(gè)想法的代碼:

    DirectoryEntry _directoryEntry = null;



private DirectoryEntry SearchRoot

        {

            get

            {

                if (_directoryEntry == null)

                {

                    _directoryEntry = new DirectoryEntry(@"LDAP://" + textBox5.Text, textBox3.Text, textBox4.Text, AuthenticationTypes.Secure);

                }

                return _directoryEntry;

            }

        }

private void GetUsers(){

  List<string> userlist = new List<string>();


            try

            {

                DirectorySearcher directorySearch = new DirectorySearcher(SearchRoot)

                {

                    Filter = "(&(objectClass=group)(SAMAccountName=" + textBox1.Text + "))"

                };



                directorySearch.PropertiesToLoad.Add("mail");

                var results = directorySearch.FindOne();


                if (results != null)

                {



                    DirectoryEntry deGroup = new DirectoryEntry(results.Path, textBox3.Text, textBox4.Text);


                    System.DirectoryServices.PropertyCollection pColl = deGroup.Properties;

                    int count = pColl["member"].Count;

                    for (int i = 0; i < count; i++)

                    {

                        string respath = results.Path;

                        string[] pathnavigate = respath.Split("CN".ToCharArray());

                        respath = pathnavigate[0];

                        string objpath = pColl["member"][i].ToString();

                        string path = respath + objpath;

                        DirectoryEntry user = new DirectoryEntry(path, textBox3.Text, textBox4.Text);

                        User userobj = User.GetUser(user);

                        userobj.EmailAddress = GetUserEmail(userobj.LoginName);

                        userlist.Add(userobj.EmailAddress);

                        user.Close();

                    }

                }

                //listBox1.DataSource = userlist;

                userlist.ForEach(item => textBox2.Text += item);

                //var t = userlist.Where(item => !string.IsNullOrEmpty(item.FirstName) || !string.IsNullOrWhiteSpace(item.FirstName)).ToList();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message + ex.InnerException + ex.StackTrace + ex.Source);

            }

}

  public string GetUserEmail(string usr)

        {

            try

            {


                string uid = textBox3.Text;

                string pwd = textBox4.Text;

                string emailProxy = "";

                string emailMail = "";

                using (var context = new PrincipalContext(ContextType.Domain, textBox5.Text, uid, pwd))

                {

                    using (UserPrincipal user = new UserPrincipal(context))

                    {

                        user.SamAccountName = usr;


                        using (var searcher = new PrincipalSearcher(user))

                        {


                            var r = searcher.FindAll();

                            foreach (var result in r)

                            {

                                DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;


                                if (de.Properties["proxyAddresses"].Value != null)

                                {

                                    List<string> tmpAddress = new List<string>();

                                    int i = 0;

                                    while (true)

                                    {

                                        try

                                        {

                                            tmpAddress.Add(de.Properties["proxyAddresses"][i].ToString());

                                            i++;

                                        }

                                        catch { break; }

                                    }

                                    string val = tmpAddress.Where(em => em.Contains("SMTP")).FirstOrDefault();


                                    if (!string.IsNullOrEmpty(val))

                                        emailProxy = val.Split(':')[1];

                                    else emailProxy = "";

                                }

                                else emailProxy = "";


                                if (de.Properties["mail"].Value != null)

                                    emailMail = de.Properties["mail"].Value.ToString();

                                else emailMail = "";

                            }

                        }

                    }

                }


                return !string.IsNullOrEmpty(emailProxy) ? emailProxy : (!string.IsNullOrEmpty(emailMail) ? emailMail : "");


            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message + ex.InnerException + ex.StackTrace + ex.Source);

                return "";

            }

        }


查看完整回答
反對(duì) 回復(fù) 2021-11-07
  • 1 回答
  • 0 關(guān)注
  • 489 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)