Sharepoint 将用户从AD导入我的自定义Web部件时出错

Sharepoint 将用户从AD导入我的自定义Web部件时出错,sharepoint,Sharepoint,我正在尝试从我的Web部件中的active directory获取用户,但我在某处遇到了问题 请帮助我,我的代码如下 using System; using System.Runtime.InteropServices; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Serialization; using Syste

我正在尝试从我的Web部件中的active directory获取用户,但我在某处遇到了问题 请帮助我,我的代码如下

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.DirectoryServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Collections;

namespace LdapTest2008
{
    [Guid("028042d8-7f77-4674-8b19-61b282e5ddf8")]
    public class LdapTest2008 : System.Web.UI.WebControls.WebParts.WebPart
    {
        public LdapTest2008()
        {
        }


        Label lblUsers = new Label();

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // TODO: add custom rendering code here.
            // Label label = new Label();
            // label.Text = "Hello World";
            // this.Controls.Add(label);
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            base.RenderControl(writer);
            StringCollection StrCollectionReturn = new StringCollection();
            string strGroup = "My Group";
           
           StrCollectionReturn= GetGroupMembers(strGroup);

           lblUsers.Text = StrCollectionReturn.ToString();
           this.Controls.Add(lblUsers);
           writer.Write(lblUsers.Text);
       
        }


        //Query Active Directory to get users from Active Directory Groups
            public StringCollection GetGroupMembers(string strGroup)

            {
                string domain = "LDAP://Domain.COM";
                
                string domainAndUsername = "Domain.COM\\username";
                string passWord = "password";
                StringCollection groupMemebers = new StringCollection(); 
                

            try

            {
                            DirectoryEntry ent = new DirectoryEntry(domain,domainAndUsername,passWord);

                            DirectorySearcher srch = new DirectorySearcher("(CN=" + strGroup + ")");
                            srch.SizeLimit = 0;
                            srch.PageSize = 1000;

                            SearchResultCollection coll = srch.FindAll();

                            foreach (SearchResult rs in coll)
                            {
                                ResultPropertyCollection resultPropColl = default(ResultPropertyCollection);
                                resultPropColl = rs.Properties;


                                foreach (Object memberColl in resultPropColl["member"])
                                {
                                    DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://"+ memberColl);

                                    System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;

                                    //getting user properties from AD

                                    object obVal = userProps["displayName"].Value;

                                    object obAcc = userProps["sAMAccountName"].Value;

                                    if (null != obVal)
                                    {

                                        groupMemebers.Add("User Name:" + obAcc.ToString() + ", User login name:" + obVal.ToString() + "<br>");
                                    }
                                }
                            }
               }
            

                        catch (Exception ex)

                        {
                            ex.GetBaseException();
                           // writer.Write(ex.Message);
                        }

                       
                           
                  return groupMemebers;
                
            }
    }

}
它给mes错误查找空值

我做错了什么

更新: 我犯了一个愚蠢的错误,忘记将目录条目对象传递给搜索者

DirectorySearcher srch = new DirectorySearcher(ent, "(CN=" + strGroup + ")"); ,
现在它的工作,但现在我得到了错误

DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://"+ memberColl);
我也尝试过通过传递域名也

DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://DomainName"+ memberColl);
但它仍然引发了System.Runtime.InteropServices.COMException类型的异常

请帮忙


谢谢。

也许strGroup是空的?调试并查看它包含的内容

还可能需要在LDAPtool中测试相同的字符串,看看它是否会产生类似的结果,如果strGroup不是空的

我认为你用错了上课的方式。您可能也错了

下面是DirectorySearcher类的示例:

下面是另一个与您得到的相同例外:

DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://DomainName"+ memberColl);