【发布时间】:2013-03-26 17:49:44
【问题描述】:
我有一个问题,找不到解决方案:
我有下面的代码来检索正在访问 Web 应用程序的电子邮件 (EmailAddress) 用户。
var pc = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain);
var user = System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(pc, System.DirectoryServices.AccountManagement.IdentityType.SamAccountName, username.ToLower());
email = user.EmailAddress;
对于某些用户(到目前为止为三个),电子邮件 (EmailAddress) 带有一个空值。
我也尝试了下面的代码,结果也是一样:
string connection = "LDAP://name.org";
DirectoryEntry entry = new DirectoryEntry(connection);
DirectorySearcher dssearch = new DirectorySearcher(entry);
dssearch.Filter = "(sAMAccountName=UserLogin)";
SearchResult sresult = dssearch.FindOne();
DirectoryEntry dsresult = sresult.GetDirectoryEntry();
if (dsresult.Properties.Count > 0)
{
if (dsresult.Properties["mail"].Count > 0)
Response.Write("email: " + dsresult.Properties["mail"][0].ToString());
}
else
Response.Write("<p>não encontrou</p>");
我怀疑这与 Exchange Server 有关,但我不能说缺乏知识。
谁能帮忙?
【问题讨论】:
标签: active-directory null account-management email-address userprincipal