Getting EmailAddress NULL from Active Directory (AccountManagement UserPrincipal)

落爺英雄遲暮 提交于 2019-12-11 04:59:47

问题


I have a problem and can not find solution:

I have the code below to retrieve the e-mail (EmailAddress) user that is accessing the web application.

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;

For some users (so far three) the e-mail (EmailAddress) comes with a null value.

I also tried the code below and the same happens:

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>");

I am suspicious that has something to do with Exchange Server, but I can not say for lack of knowledge.

Can anyone help?


回答1:


The system is hosted on an architecture that uses forests and tree of domains.

The implemented code accesses the AD of context in which it is running.

In this case, the AD responsible for the context of the application was not updated. The users who have problems were not with the e-mail address filled this AD.

And when it consulted the main AD, with some tool for this, the information was there correctly, which caused confusion.

After some investigations was detected confusion.

That's it. Problem solved!

Thanks for the cooperation!



来源:https://stackoverflow.com/questions/15644025/getting-emailaddress-null-from-active-directory-accountmanagement-userprincipal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!