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?