我正在尝试从我的webpart中的活动目录中获取用户,但我在某处感到很沮丧 请帮帮我,我的代码如下。
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;
}
}
}
调试此代码时,我被困在这里:
DirectorySearcher srch = new DirectorySearcher("(CN=" + strGroup + ")");
它给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
请帮忙。
谢谢。
答案 0 :(得分:0)
也许strGroup是空的?调试并查看它包含的内容。
如果strGroup不为空,也可能想在LDAPtool中测试相同的字符串以查看它是否会显示任何结果。
好吧,我觉得你错误地使用了这些类。你是LDAP字符串也可能是错的。
以下是DirectorySearcher类的示例: http://msdn.microsoft.com/en-us/library/ms180885(VS.80).aspx
这是另一个与你得到的相同的例外情况: http://forums.asp.net/p/322041/322460.aspx