Getting EmailAddress NULL from Active Directory (A

2019-08-15 19:25发布

问题:

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!