[英]Problem querying Active Directory for emails when User has none
I'm querying our Active Directory with the following code:我正在使用以下代码查询我们的 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()
};
}
}
As you can see, I'm trying to convert the results into an IEnumerable list of ADUser (my own class defined like this):如您所见,我正在尝试将结果转换为 ADUser 的 IEnumerable 列表(我自己的 class 定义如下):
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; }
}
However, my code crashes anytime I hit a user who does not have an email entry.但是,只要我遇到没有 email 条目的用户,我的代码就会崩溃。 It seems as though the SearchResult does not contain a property for mail when the user has no email. Is there a way to get the result to return the property with a null or empty value instead?当用户没有 email 时,SearchResult 似乎不包含邮件属性。有没有办法让结果返回带有 null 或空值的属性?
Thanks for your help.谢谢你的帮助。
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;
}
Edit after comment: 评论后编辑:
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
}
You should be able to use the ?? 您应该可以使用?? "null coalescing" operator with the email string. 带有电子邮件字符串的“空合并”运算符。
string s1 = null; string s2 = s1 ?? "S1 was null." // Prints: s2 == "S1 was null."
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.