0

我的 asp.net mvc5 Web 应用程序中有以下代码:-

List<DomainContext> results = new List<DomainContext>();
using (var context = new PrincipalContext(ContextType.Domain, ADServerName, ADusername, ADpassword))
            using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
            {
                var searchResults = searcher.FindAll();



                foreach (Principal p in searchResults)
                {
                  {
                    DomainContext dc = new DomainContext();
                    dc.DisplayName = p.DisplayName;
                    dc.UserPrincipalName = p.UserPrincipalName;
                    dc.Name = p.Name;
                    dc.SamAccountName = p.SamAccountName ;
                    dc.DistinguishedName =     p.DistinguishedName;

                    results.Add(dc);

我可以获取这些广告信息,例如显示名称、名称等,但我也可以使用上面的代码获取这些信息:-

  • 电子邮件地址。
  • 电话。
  • 经理

谢谢?

4

2 回答 2

1

下面我更新了您现有的代码以使用 UserPrincipal。

List<UserPrincipal> results = new List<UserPrincipal>();
using (var context = new PrincipalContext(ContextType.Domain, ADServerName, ADusername, ADpassword))
            using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
            {
                var searchResults = searcher.FindAll();
                foreach (Principal p in searchResults)
                {
                  {
                    UserPrincipal userPrincipal = p as UserPrincipal;
                    if (userPrincipal != null)
                        results.Add(userPrincipal);

在这里,我添加了代码来向您展示如何在更高级别获得所需的其他属性。

//Get information you need from UserPrincipal..
var matchingUsers = results.Where(p => p.DisplayName.ToLower() == "bob");
foreach (var matchedUser in matchingUsers)
{   
  string telephone = matchedUser.VoiceTelephoneNumber;
  string email = matchedUser.EmailAddress;
  var directoryEntry = matchedUser.GetUNderlyingObject() as DirectoryEntry;
  string manager = directoryEntry.Properties["manager"] as string;
}
于 2015-01-19T18:40:58.347 回答
1

您需要用户名、first、last 和 smtp。如果你有它们,这里是代码:

var adService = new DirectorySearcher(new DirectoryEntry());
    adService.Filter = "(&(objectClass=user)(anr=LOGON))";
    adService.PropertiesToLoad.Add("FirstName");
    adService.PropertiesToLoad.Add("LastName");
    adService.PropertiesToLoad.Add("SMTP");
    return adService.FindOne();
于 2015-01-17T07:21:17.643 回答