当User没有时,查询Active Directory中的邮件出现问题

本文关键字:问题 Directory Active User 查询 | 更新日期: 2023-09-27 18:07:39

我正在用以下代码查询我们的活动目录:

            using (DirectorySearcher search = new DirectorySearcher(de))
            {
                search.PropertiesToLoad.Add("cn");
                search.PropertiesToLoad.Add("employeeid");
                search.PropertiesToLoad.Add("employeenumber");
                search.PropertiesToLoad.Add("distinguishedname");
                search.PropertiesToLoad.Add("mail");
                search.Filter = @"(&(objectClass=user)(employeeid=*)(employeenumber=*))";
                search.PageSize = 3000;
                SearchResultCollection src = search.FindAll();
                foreach (SearchResult rec in src)
                {
                    yield return new ADUser()
                    {
                        Name = rec.Properties["cn"][0].ToString(),
                        Path = rec.Properties["distinguishedname"][0].ToString(),
                        Acctno = rec.Properties["employeeid"][0].ToString(),
                        Personno = rec.Properties["employeenumber"][0].ToString(),
                        Email = rec.Properties["mail"][0].ToString()
                    };
                }
            }

正如您所看到的,我试图将结果转换为ADUser(我自己定义的类)的IEnumerable列表:

    public class ADUser
    {
        public string Name { get; set; }
        public string Path { get; set; }
        public string Acctno { get; set; }
        public string Personno { get; set; }
        public string Email { get; set; }
    }

然而,每当我遇到没有电子邮件条目的用户时,我的代码就会崩溃。当用户没有电子邮件时,SearchResult似乎不包含邮件属性。是否有一种方法来获得结果,以返回一个空值或空值的属性?

谢谢你的帮助。

当User没有时,查询Active Directory中的邮件出现问题

注释后编辑:

if (rec.Properties.Contains("mail") && rec.Properties["mail"] != null)
{
  Email = rec.Properties["mail"][0].ToString()
}
else
{
   Email = "No mail"; # or Email = ""; if you want no text returned 
}
using System;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.DirectoryServices;
using System.Collections;
using System.DirectoryServices.AccountManagement;
using PropertyCollection = System.DirectoryServices.PropertyCollection;

    public class UserEmailInfo
    {
        public UserEmailInfo()
        {
        }
        public string DisplayName { get; set; }
        public string DisplayEmail { get; set; }
    }

    [WebMethod]
    public string GetADUserDisplayNameAndEmailAddress(string displayName)
    {
        //var search = new DirectorySearcher(new DirectoryEntry("LDAP://domain.local"));
        List<UserEmailInfo> displayNameEmailAddress = new List<UserEmailInfo>();
        var search = new DirectorySearcher(new DirectoryEntry("LDAP://DC=domain,DC=local"));
        string ldapfilter = "(&(ObjectClass=user)(objectCategory=person)(name={0}))";
        search.Filter = String.Format(ldapfilter, displayName);
        search.PropertiesToLoad.Add("displayname");
        search.PropertiesToLoad.Add("mail");
        SearchResult result = search.FindOne();
            if (result == null)
            {
                displayNameEmailAddress.Add(new UserEmailInfo()
                {
                    DisplayName = "none",
                    DisplayEmail = "none"
                });
            }
            else
            {
                if (result.Properties.Contains("mail") && result.Properties["mail"] != null)
                {
                    displayNameEmailAddress.Add(new UserEmailInfo()
                    {
                        DisplayName = result.Properties["displayname"][0].ToString(),
                        DisplayEmail = result.Properties["mail"][0].ToString()
                    });
                }
            }

            
       
                 
        //return emailAddress;
        string myJsonString = (new JavaScriptSerializer()).Serialize(displayNameEmailAddress);
        return myJsonString;
    }

你应该能够使用??"空合并"操作符与电子邮件字符串。

string s1 = null; string s2 = s1 ?? "S1 was null." // Prints: s2 == "S1 was null."