protected void Page_Load(object sender, EventArgs e)
{
string path = "LDAP://"; // Path for active directory.
System.DirectoryServices.DirectoryEntry de = new System.DirectoryServices.DirectoryEntry(path);
System.DirectoryServices.DirectorySearcher deSearch = new System.DirectoryServices.DirectorySearcher(de);
deSearch.PropertiesToLoad.Add("mail");
deSearch.PropertiesToLoad.Add("telephoneNumber");
string strUserName = System.Web.HttpContext.Current.User.Identity.Name;
strUserName = strUserName.Remove(0, 10);
deSearch.Filter = ("(sAMAccountName= " + strUserName + ")");
deSearch.SearchScope = SearchScope.Subtree;
SearchResult results = deSearch.FindOne();
TextBox1.Text = results.Properties["mail"][0].ToString();
if ((results.Properties["telephoneNumber"].Count != 0))
{
TextBox1.Text = results.Properties["telephoneNumber"][0].ToString();
}
else
{
Response.Write("Error: There is no entry in telephoneNumber field of the Active Directory for the current user");
}
}
No comments:
Post a Comment