GetAuthorizationGroups() 抛出异常
- 作者: Modest_
- 来源: 51数据库
- 2022-10-28
问题描述
PrincipalContext context = new PrincipalContext(ContextType.Domain, "ipofmachine", "DC=xyz,DC=org", "username", "Password");
UserPrincipal userPrinciple = UserPrincipal.FindByIdentity(context, "User0");
var groups = userPrinciple.GetAuthorizationGroups();
if (userPrinciple != null)
{
foreach (GroupPrincipal gp in groups)
{
//some thing
}
}
我需要给予任何许可吗?在一些博客中,我了解到如果没有设置为包含 SID 历史记录的用户,那么这将正常工作(但我认为您无法编辑组的 sid 值)
Is there any permission that I need to give? In some of the blogs I learned that if there are no users which are set to include the SID history then this will work fine (but i think you can not edit the sid values of the groups)
推荐答案
我发现将域用户添加到本地组时存在问题,但后来该域用户从 Active Directory 中删除.该本地组的状态是使用 SID 而不是显示为成员的域用户名.
I've found there's an issue when you add a domain user to a local group, but later that domain user is deleted out of Active Directory. The state of that local group is that instead of a domain username showing up as a member, the SID is used instead.
但是!
该 SID 不再存在于 Active Directory 中,这导致事情变得繁荣起来.
That SID doesn't exist in Active Directory anymore causing things to go boom.
当然,弹出 NoMatchingPrincipalException 的原因可能有很多,因此此代码提供了一种解决方法.它来自 MSDN 上的一篇很棒的帖子.下面的代码是在这里找到的修改版本:
Of course there could be many other reasons for an NoMatchingPrincipalException to pop up, so this code provides a workaround for that. It comes from a terrific post on MSDN. The code below is a modified version found here:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/9dd81553-3539-4281-addd-3eb75e6e4d5d/getauthorizationgroups-fails-with-nomatchingprincipalexception
public static IEnumerable<Principal> getAuthorizationGroups(UserPrincipal user)
{
PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();
List<Principal> ret = new List<Principal>();
var iterGroup = groups.GetEnumerator();
using (iterGroup)
{
while (iterGroup.MoveNext())
{
try
{
Principal p = iterGroup.Current;
Console.WriteLine(p.Name);
ret.Add(p);
}
catch (NoMatchingPrincipalException pex)
{
continue;
}
}
}
return ret;
}
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
