我在自定义 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/