c# - Active Directory RoleProvider -Principal.IsMemberOf 抛出PrincipalOperationException

标签 c# .net asp.net-mvc active-directory roleprovider

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

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[];
}

它仅检查我的应用程序中使用的角色白名单。问题是,如果用户不是某个角色的成员,当

时我会收到 PrincipalOperationException
if (user.IsMemberOf(adGroup))

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

编辑: 另外,如果我调用 user.GetAuthorizationGroups() 并尝试循环结果,我会收到 COMException - 指定的目录服务属性或值不存在。

最佳答案

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

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

查看此MSDN KB了解更多详情。

关于c# - Active Directory RoleProvider -Principal.IsMemberOf 抛出PrincipalOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11558587/

相关文章:

c# - 在 C# 中播放声音 byte[]

asp.net-mvc - 自定义 ActionResult 和 viewbag

jquery - StrengthJS 密码强度在一个函数中有效,但在多个函数中不起作用

c# - 值 'XXX.yyyyy' 的格式无效

c# - List<T>.AddRange 实现次优

c# - 循环地将字符串分割成锯齿状数组

c# - 如何将这些 LINQ 结果加载到我的 ViewModel 类中?

c# - 功能区按钮的 `onAction` 设计模式

c# - 在 Windows Phone 8.1 中保存前旋转图像

c# - 如何将我的代码从 C# 转换为 Python?