C# UserPrinciples.GetAuthorizationGroups枚举组时出错(1301)。无法解析组的SID
背景: 我使用UserPrincipal.GetAuthorizationGroups检查两个不同应用程序中的权限已有一段时间了。几年来他们一直工作得很好。最近,一些用户收到标题System.DirectoryServices.AccountManagement.PrincipalOperationException中提到的错误,而其他用户没有收到。我怀疑它可能与在Windows Server 2012上运行的新添加的域控制器有关,因为问题是在添加后的第二天开始出现的。完整错误如下所示: 例外情况: System.DirectoryServices.AccountManagement.PrincipalOperationException: 枚举组时发生错误1301。该集团的 无法解析SID 位于System.DirectoryServices.AccountManagement.SidList.translateSidString目标,IntPtr[]pSids 在System.DirectoryServices.AccountManagement.SidList..ctorSID\u和\u ATTR[]sidAndAttr 在System.DirectoryServices.AccountManagement.AuthZSet..ctorByte[]userSid,NetCred凭据, ContextOptions ContextOptions、字符串flatUserAuthority、StoreCtx userStoreCtx、Object userCtxBase 在System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOfAZ…p 位于System.DirectoryServices.AccountManagement.UserPrincipal.GetAuthorizationGroups 问题:C# UserPrinciples.GetAuthorizationGroups枚举组时出错(1301)。无法解析组的SID,c#,active-directory,C#,Active Directory,背景: 我使用UserPrincipal.GetAuthorizationGroups检查两个不同应用程序中的权限已有一段时间了。几年来他们一直工作得很好。最近,一些用户收到标题System.DirectoryServices.AccountManagement.PrincipalOperationException中提到的错误,而其他用户没有收到。我怀疑它可能与在Windows Server 2012上运行的新添加的域控制器有关,因为问题是在添加后的第二天开始出现的。完整错误如下所示: 例外情
如何解决此问题?我使用DirectorySearcher找到了一个替代方案: 检查以下答案:
MS fix我也有同样的例外。如果有人不想使用LDAP,请使用以下代码。因为我有嵌套的组,我使用GetMemberstrue,它的时间比GetMembers长一点 或者从这里下载补丁,如@Tilo所说:
您的问题是否与此错误类似:?是的,只是我不确定Microsoft Endpoint Protection更新是如何涉及的。非常好-这为我解决了问题!感谢Skeletank。我认为这解决了我的问题,但当用户只属于1个组时,这会抛出:无法将“System.String”类型的对象强制转换为“System.object[]“@skeletank您是否知道如何处理此实例?@Chris它在哪一行失败?@skeletank在尝试遍历user.groupName中的groupName并实际执行memberOf查询时。它适用于具有多个组的用户,但如果他们只有一个组,则会中断。我已经实现了一个try-catch来解决这个问题,但是我想知道我是否可以使用您提供的这个漂亮的代码块,并进行一些小的调整?谢谢
var allDomains = Forest.GetCurrentForest().Domains.Cast<Domain>();
var allSearcher = allDomains.Select(domain =>
{
DirectorySearcher searcher = new DirectorySearcher(
new DirectoryEntry("LDAP://" + domain.Name));
searcher.Filter = String.Format(
"(&(&(objectCategory=person)(objectClass=user)(userPrincipalName=*{0}*)))",
"Current User Login Name");
return searcher;
}
);
var directoryEntriesFound =
allSearcher.SelectMany(searcher =>
searcher.FindAll()
.Cast<SearchResult>()
.Select(result => result.GetDirectoryEntry()));
var memberOf = directoryEntriesFound.Select(entry =>
{
using (entry)
{
return new
{
Name = entry.Name,
GroupName = ((object[])entry.Properties["MemberOf"].Value)
.Select(obj => obj.ToString())
};
}
}
);
foreach (var user in memberOf)
{
foreach (var groupName in user.GroupName)
{
if (groupName.Contains("Group to Find"))
{
// Do something if the user is in that group
}
}
}
public bool IsMember(UserPrincipal user, string groupName)
{
try
{
var context = new PrincipalContext(ContextType.Domain, Environment.UserDomainName);
var group = GroupPrincipal.FindByIdentity(context, groupName);
if (group == null)
{
//Not exist
}
else
{
if (group.GetMembers(true).Any(member => user.SamAccountName.ToLower() == member.SamAccountName.ToLower()))
{
return true;
}
}
}
catch (Exception exception)
{
//exception
}
return false;
}