我在自定义Active中创建了以下方法:
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[];
}它只检查在我的应用程序中使用的角色的白列表。问题是,如果用户不是某个角色的成员,则当
if (user.IsMemberOf(adGroup))行被执行。如果用户不在组中,我希望这只返回‘`false’。这里出什么问题了?
编辑:顺便说一下,如果我调用user.GetAuthorizationGroups()并试图遍历结果,就会得到一个COMException --指定的目录服务属性或值不存在。
发布于 2012-07-20 04:26:58
Principal.IsMemberOf()和user.GetAuthorizationGroups()都使用tokenGroups属性来确定组成员资格。
您需要确保将用于运行程序的帐户添加到Builtin\Windows Authorization Access Group中,以便访问tokenGroups属性。
有关更多细节,请参见此MSDN KB。
发布于 2012-07-19 15:22:49
我设法解决了以下问题:
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[];
}然而,它并不完全是有效的,因为它把一个组的所有成员都拉回来了。我相信有一个更好的解决方案,我的问题,并希望有人会张贴在这里!
https://stackoverflow.com/questions/11558587
复制相似问题