AD PropertiesToLoad

以下方式若想改寫成抓取全部 , 想請教一些疑問

撈出ActiveDirectory聯絡人

按下button1 撈出單個使用者:

  private  void button1_Click( object sender, EventArgs e)

        {

             string uid =  " GGYY "// Domain帳號

             string pwd =  " 12345 ";   // 密碼

             string domain = GetDomainName(domain_txt.Text);   // 取得Domain

             string QueryString =  " LDAP:// " + domain;

            DirectoryEntry ldapConnection =  new DirectoryEntry(QueryString, uid, pwd);    // 使用DirectoryEntry  連入ActiveDirectory

            DirectorySearcher Searcher =  new DirectorySearcher(ldapConnection);

             string value =  string.Empty;

             string target = target_txt.Text;       

           try

            {

                 Searcher.Filter =  " (SAMAccountName= " + target +  " ) ";     // 過濾條件  *有點搞不懂過濾的條件寫法該如何解釋,不知哪位大大能夠幫忙解釋

                 Searcher.PropertiesToLoad.Add( " SAMAccountName ");    // 帳號

                 Searcher.PropertiesToLoad.Add( " Name ");    //  全名

                 Searcher.PropertiesToLoad.Add( " displayName ");    // ActiveDirectory的顯示名稱

                 Searcher.PropertiesToLoad.Add( " mail ");            // ActiveDirectory的信箱

                 Searcher.PropertiesToLoad.Add( " description ");

                 Searcher.PropertiesToLoad.Add( " department ");     // ActiveDirectory的信箱

                 Searcher.PropertiesToLoad.Add( " company ");

                 Searcher.PropertiesToLoad.Add( " phsicalDeliveryOfficeName ");

                 Searcher.PropertiesToLoad.Add( " userPrincipalName ");   // 使用者帳號+Domain

                 Searcher.PropertiesToLoad.Add( " telephoneNumber ");       

                 Searcher.PropertiesToLoad.Add( " givenName ");   // first name

                 Searcher.PropertiesToLoad.Add( " sn ");

                SearchResult result = Searcher.FindOne();        // 若抓出全部使用者,應該是寫成這個方式 SearchResultCollection result = Searcher.FindAll( );

                 if (result !=  null)

                {

                     foreach ( string key  in result.Properties.PropertyNames)

                    {

                         foreach (Object propValue  in result.Properties[key])

                        {

                            value += key +  "  =  " + propValue +  " \r\n ";

                            textBox1.Text += key +  "  =  " + propValue +  " \r\n ";

                        }

                    }

                }

                 else

                {

                    MessageBox.Show( " 使用者不存在 ");

                }

            }

             catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

             finally

            {

                Searcher.Dispose();

                ldapConnection.Dispose();

            }   

        }

转载于:https://www.cnblogs.com/hishanghai/archive/2012/07/27/2611947.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值