c# - GroupPrincipal 方法 FindByIdentity 抛出奇怪的异常

标签 c# asp.net .net sharepoint-2010 active-directory

我正在尝试按组名获取所有用户并将其显示在 Sharepoint Web 部件中。 adGroupName 类似 = "CompanyGroup"。

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

异常(exception):

In order to perform this operation a successful bind must be completed on the connection

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

堆栈跟踪:

       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 {“发生操作错误。\r\n”}

最佳答案

答案在博客中找到:

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 ...
}

关于c# - GroupPrincipal 方法 FindByIdentity 抛出奇怪的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7969425/

相关文章:

c# - 如何克隆对象

c# - EF Core 和 MySql 查询太慢

c# - 为什么在为行单元格设置文本时得到 'System.ArgumentOutOfRangeException'?

使用自定义 Facebook 和 Google 按钮进行 ASP.NET Identity 登录

asp.net - 安装serilog并在ASP .NET 4.7.1 WebAPI中进行配置

c# - 我可以在安装 NuGet 包时解决添加这些 .dll 问题吗?

c# - C#中如何从数据库中获取图片

c# - 如何使用 Ninject InRequestScope 处理异步调用?

C# 抽象类命名约定

asp.net - 使用 System.Threading.ThreadPool 时没有内容的电子邮件