【发布时间】:2018-02-09 19:15:14
【问题描述】:
我目前有检查用户是否属于单个 AD 组的代码,但是如何检查此用户是否属于多个 AD 组。 下面是我必须检查单个组的代码
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, Environment.UserDomainName);
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, Environment.UserName);
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "ADGROUP1");
if (user != null)
{
if (user.IsMemberOf(group))
{
//Enable certain Form Buttons and objects for IT Users
authTbox.Visible = true;
}
}
如何检查同一用户是否属于 ADgroup2 、 ADGroup3 等。
我在论坛和谷歌搜索了这里,但找不到有效的解决方案。实现此目的的一种方法是定义多个组并在 if 子句中使用 OR 来检查所有组..见下文
前:
GroupPrincipal group1 = GroupPrincipal.FindByIdentity(ctx, "ADGROUP1");
GroupPrincipal group2 = GroupPrincipal.FindByIdentity(ctx, "ADGROUP2");
if (user != null)
{
if (user.IsMemberOf(group) ||user.IsMemberOf(group1) || user.IsMemberOf(group2) )
{
//Enable certain Form Buttons and objects for IT Users
authTbox.Visible = true;
}
}
由于我必须搜索 10 个这样的组,我只是想知道是否有有效的方法来实现这一目标。
【问题讨论】:
标签: c# winforms active-directory