UserPrincipals.GetAuthorizationGroups枚举组时发生错误(1301).该组';无
本文关键字:该组 1301 错误 枚举 GetAuthorizationGroups UserPrincipals | 更新日期: 2023-09-27 18:11:55
背景:
我已经使用UserPrincipal.GetAuthorizationGroups
检查两个不同应用程序的权限有一段时间了。他们几年来一直工作得很好。最近,一些用户收到了标题中提到的错误(System.DirectoryServices.AccountManagement.PrincipalOperationException
(,而其他用户则没有。我怀疑它可能与在Windows Server 2012上添加的新域控制器有关,因为问题在添加后的第二天就开始了。完整错误如下所示:
异常:
System.DirectoryServices.AccountManagement.PrincipalOperationException:枚举组时发生错误(1301(。该小组的无法解析SID。
位于System.DirectoryServices.AccountManagement.SidList.PranslateSids(字符串目标,IntPtr[]pSids(位于System.DirectoryServices.AccountManagement.SidList.ctor(SID_AND_ATTR[]sidAndAttr(
在System.DirectoryServices.AccountManagement.AuthZSet.ctor(Byte[]userSid,NetCred凭据,ContextOptions ContextOptions,String flatUserAuthority,StoreCtx userStoreCtx,Object userCtxBase(
位于System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOfAZ…p(
位于System.DirectoryServices.AccountManagement.UserPrincipal.GetAuthorizationGroups
问题:
我该如何解决这个问题?
我找到了使用DirectorySearcher
:的替代方案
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
}
}
}
检查此答案:UserPrincipals.GetAuthorizationGroups枚举组时发生错误(1301(。升级到Server 2012域控制器后
MS修复http://support.microsoft.com/kb/2830145
我也有同样的异常。如果有人不想使用"LDAP",请使用以下代码。因为我有嵌套的组,我使用GetMembers(true(,它的时间比GetMembers长一点。
或者像@Tilo所说的那样从这里下载修复程序:http://support.microsoft.com/kb/2830145
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;
}