首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Active - Principal.IsMemberOf抛出PrincipalOperationException

Active - Principal.IsMemberOf抛出PrincipalOperationException
EN

Stack Overflow用户
提问于 2012-07-19 10:12:54
回答 2查看 2.3K关注 0票数 2

我在自定义Active中创建了以下方法:

代码语言:javascript
复制
public override string[] GetRolesForUser(string username)
{
    ArrayList results = new ArrayList();
    using (var principalContext = new PrincipalContext(
             ContextType.Domain, null, domainContainer))
    {
        var user = UserPrincipal.FindByIdentity(
             principalContext, IdentityType.SamAccountName, username);
        foreach (string acceptibleGroup in GroupsToInclude)
        {
            GroupPrincipal adGroup = GroupPrincipal.FindByIdentity(
                 principalContext, acceptibleGroup);
            if (user.IsMemberOf(adGroup))
                results.Add(acceptibleGroup);
        }
    }
    return results.ToArray(typeof(string)) as string[];
}

它只检查在我的应用程序中使用的角色的白列表。问题是,如果用户不是某个角色的成员,则当

代码语言:javascript
复制
if (user.IsMemberOf(adGroup))

行被执行。如果用户不在组中,我希望这只返回‘`false’。这里出什么问题了?

编辑:顺便说一下,如果我调用user.GetAuthorizationGroups()并试图遍历结果,就会得到一个COMException --指定的目录服务属性或值不存在。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-20 04:26:58

Principal.IsMemberOf()user.GetAuthorizationGroups()都使用tokenGroups属性来确定组成员资格。

您需要确保将用于运行程序的帐户添加到Builtin\Windows Authorization Access Group中,以便访问tokenGroups属性。

有关更多细节,请参见此MSDN KB

票数 3
EN

Stack Overflow用户

发布于 2012-07-19 15:22:49

我设法解决了以下问题:

代码语言:javascript
复制
public override string[] GetRolesForUser(string username) 
{ 
    ArrayList results = new ArrayList(); 

    using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, null, domainContainer))
        {
            UserPrincipal user = UserPrincipal.FindByIdentity(principalContext, IdentityType.SamAccountName, username);
            foreach (string acceptibleGroup in GroupsToInclude)
            {
                GroupPrincipal p = GroupPrincipal.FindByIdentity(principalContext, IdentityType.SamAccountName, acceptibleGroup);
                if (p.GetMembers().Contains(user))
                    results.Add(acceptibleGroup);
            }
        } 

    return results.ToArray(typeof(string)) as string[]; 
}

然而,它并不完全是有效的,因为它把一个组的所有成员都拉回来了。我相信有一个更好的解决方案,我的问题,并希望有人会张贴在这里!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11558587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档