Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

asp.net c# get user email address from AD

using System.Linq;
using System.DirectoryServices;

string username = User.Identity.Name.Split('').Last();
string email = GetMail(username);

static string GetMail(string user)
{
  using (var connection = new DirectoryEntry())
  {
    using (var search = new DirectorySearcher(connection)
      {
        Filter = "(samaccountname=" + user + ")",
        PropertiesToLoad = {"mail"},
      })
    {
      return (string) search.FindOne().Properties["mail"][0];
    }
  }
}
 
PREVIOUS NEXT
Tagged: #user #email #address #AD
ADD COMMENT
Topic
Name
1+6 =