GroupPrincipal方法FindByIdentity抛出奇怪的异常

本文关键字:异常 方法 FindByIdentity GroupPrincipal | 更新日期: 2023-09-27 18:13:27

我试图通过组名获得所有用户并在sharepoint webpart中显示它。adGroupName = "CompanyGroup".

GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, adGroupName);
异常:

要执行此操作,必须在连接

上成功完成绑定。

为什么是这样,我做错了什么?

堆栈跟踪:

       at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_AdsObject()
   at System.DirectoryServices.PropertyValueCollection.PopulateList()
   at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
   at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
   at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
   at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()
   at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
   at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, IdentityType identityType, String identityValue)
   at System.DirectoryServices.AccountManagement.GroupPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue)
   at NewCo.Intranet.Common.DataAccess.ADUserManager.GetUserForGroup(String adGroupName, Boolean recursive)
   at NewCo.Intranet.Common.DataAccess.ADUserManager.GetMemberForCurrentSite()
   at NewCo.Intranet.Components.WebParts.ADGroupMembers.ADGroupMembersUserControl.Page_Load(Object sender, EventArgs e)

DirectoryServicesCOMException {"An operation error occurred.'r'n"}

GroupPrincipal方法FindByIdentity抛出奇怪的异常

答案在博客中找到:

http://sharepoint-tweaking.blogspot.com/2007/12/impersonation-in-aspnet-causes.html

解决方案是使用代码:

using (HostingEnvironment.Impersonate()) {
     // This code runs as the application pool user
     DirectorySearcher searcher ...
}

您是否使用这样的东西来获取您的上下文?

/* Retreiving a principal context
*/
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "MachineIP:389", "dc=dom,dc=fr", "jpb", "root.123");

您可以将MachineIP替换为您的域名或您的DC之一的DNS名称。