【问题标题】:Problem querying Active Directory for emails when User has none当用户没有电子邮件时,在 Active Directory 中查询电子邮件时出现问题
【发布时间】:2011-09-06 20:00:47
【问题描述】:

我正在使用以下代码查询我们的 Active Directory:

            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 不包含邮件属性。有没有办法让结果返回具有 null 或空值的属性?

感谢您的帮助。

【问题讨论】:

    标签: c# active-directory


    【解决方案1】:

    评论后编辑:

    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 
    
    }
    

    【讨论】:

    • 您确定崩溃不是在 OP 尝试访问空数组的第一个元素时发生的吗?
    • 谢谢,这样就可以了。我希望 AD 执行起来更像一个真正的查询,并且至少返回具有空值的属性,而不是根本不返回任何属性。
    【解决方案2】:
    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;
        }
    

    【讨论】:

    • 使用 if (result == null) 检查 search.FindOne 在 AD 中的搜索方法无法找到与 webmethod 调用的显示名称相匹配时返回 null 的时间
    【解决方案3】:

    您应该能够使用 ??带有电子邮件字符串的“空合并”运算符。

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

    【讨论】:

    • 问题很可能来自索引器 [0],电子邮件字段可能为空,所以 ??不会工作
    • 我在想:var email = rec.Properties["mail"][0].ToString() ?? "" 会保释他。
    • ToString() 永远不会返回 null 所以它不会
    猜你喜欢
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多