【发布时间】:2014-02-04 22:59:57
【问题描述】:
这与this post 相关,我正在尝试做同样的事情。因为我无法让它发挥作用,所以我正在尝试一种不同的方法。我的目标是让所有用户都在一个集合中,并循环通过集合加载参数,以实现类似于前一个帖子的存储过程。
这次我再次尝试 AccountManagement 类,我可以获取用户的所有属性,但是这次我不知道获取组或组描述的语法。我假设我需要简单地加载来自 UserPrincipal 的某种类型的集合,例如 up.GetGroups() 并枚举它,但我在语法上苦苦挣扎。使用该代码,我还需要可以访问描述的内容。
PrincipalContext AD = new PrincipalContext(ContextType.Domain, "[my domain", "[my path]");
UserPrincipal ADUser = new UserPrincipal(AD);
PrincipalSearcher ps = new PrincipalSearcher();
ps.QueryFilter = ADUser;
PrincipalSearchResult<Principal> result = ps.FindAll();
foreach (Principal p in result)
using (UserPrincipal up = (UserPrincipal)p)
{
if (up.AccountExpirationDate.HasValue)
Debug.WriteLine(up.AccountExpirationDate.ToString());
if (up.AccountLockoutTime.HasValue)
Debug.WriteLine(up.BadLogonCount.ToString());
if (up.DisplayName != null)
Debug.WriteLine(up.DisplayName.ToString());
if (up.DistinguishedName != null)
Debug.WriteLine(up.DistinguishedName.ToString());
if (up.EmailAddress != null)
Debug.WriteLine(up.EmailAddress.ToString());
if (up.EmployeeId != null)
Debug.WriteLine(up.EmployeeId.ToString());
if (up.Enabled.HasValue)
if (up.Enabled == true)
Debug.WriteLine("User is active");
else
Debug.WriteLine("User is deactivated");
if (up.GivenName != null)
Debug.WriteLine(up.GivenName.ToString());
if (up.LastBadPasswordAttempt.HasValue)
Debug.WriteLine(up.LastBadPasswordAttempt.ToString());
if (up.LastLogon.HasValue)
Debug.WriteLine(up.LastLogon.ToString());
if (up.LastPasswordSet.HasValue)
Debug.WriteLine(up.LastPasswordSet.ToString());
if (up.MiddleName != null)
Debug.WriteLine(up.MiddleName.ToString());
if (up.Name != null)
Debug.WriteLine(up.Name.ToString());
if (up.PasswordNeverExpires != null)
if (up.PasswordNeverExpires == true)
Debug.Print("User Password Never Expires");
else
Debug.WriteLine("User Password Expires");
if (up.SamAccountName != null)
Debug.WriteLine(up.SamAccountName.ToString());
if (up.Sid != null)
Debug.WriteLine(up.Sid.ToString());
if (up.Surname != null)
Debug.WriteLine(up.Surname.ToString());
if (up.UserPrincipalName != null)
Debug.WriteLine(up.UserPrincipalName.ToString());
if (up.VoiceTelephoneNumber != null)
Debug.WriteLine(up.VoiceTelephoneNumber.ToString());
}
我尝试使用GroupPrincipal,但正如它所写的那样,我可以看到描述字段,但我看不到其他任何内容。我尝试了该代码:
//PrincipalContext AD = new PrincipalContext(ContextType.Domain, "[my domain]", "[my path]");
GroupPrincipal theGroup = new GroupPrincipal(AD);
PrincipalSearcher gps = new PrincipalSearcher(theGroup);
foreach (var found in gps.FindAll())
{
if (found.Description != null)
{
Debug.WriteLine(found.Description.ToString());
}
if (found.DisplayName != null)
{
Debug.WriteLine(found.DisplayName.ToString());
}
}
这段代码可以很好地获取组的描述,但我看不到其他任何内容,因为所有其他字段都是空的。
我们将不胜感激任何和所有的帮助。
【问题讨论】:
-
除了名称和描述之外,您还想从群组中获得什么?
-
不幸的是,我需要上面第一段代码中列出的所有字段。除了我遇到的问题之外,似乎即使我从这篇文章中解决了问题,其中一些字段在 UserPrincipal 中也将不可用。我将不得不在 UserPrincipal 和“ExtensionGet”方法之间使用某种类型的混合。我对此了解不多,但我看到其他人在其他帖子中谈论他们是如何解决它的,当我到达那里时,我会越过那座桥。
-
您为用户列出的一半内容不适用于组,只会为空,其余部分无论如何都不会真正使用,仍然为空。我相信您的代码没有任何问题,您的问题只是您的组不存在这些信息。
-
@Ashigore,我确定我在回答中遇到了这个问题。我找不到或不知道的最大的事情是
PrincipalSearchResult<Principal> userGroups = CurrentUser.GetGroups();因为我不知道如何访问每个用户所属的组。一旦我知道了,我就没事了,剩下的就可以解决了。但是您提到的问题仍然存在,我仍在努力寻找解决方案。无论哪种方式,我仍然需要这段代码才能进入下一步,希望很快就能完成。谢谢
标签: c# active-directory