how to get userdetails from Active Directory based on username using asp.net
Introduction:
Here I will explain how to get userdetails from Active directory based on username using asp.net
Description:
I got requirement like to get user details from Active directory based on username.
Here I will explain how to get userdetails from Active directory based on username using asp.net
Description:
I got requirement like to get user details from Active directory based on username.
For that first create one new website after that right click on website and select Add Reference option after that select System.DirectoryServices from .NET tab and click ok now directory servicesreference has added to our application do you know why we have added this directory service to our application because by using this service we can get userdetails from Active directory.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="aspdotnet1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Enter Username:
</td>
<td>
<asp:TextBox ID="txtusername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</td>
</tr>
<tr>
<td align="right">First Name:
</td>
<td>
<asp:Label ID="lblfname" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td align="right">Last Name:
</td>
<td>
<asp:Label ID="lbllname" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td align="right">Email:
</td>
<td>
<asp:Label ID="lblemail" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td align="right">Display name:
</td>
<td>
<asp:Label ID="lbldisplayname" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td align="right">Description:
</td>
<td>
<asp:Label ID="lbldescription" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td align="right">Office:
</td>
<td>
<asp:Label ID="lblofice" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td align="right">Telephone number:
</td>
<td>
<asp:Label ID="lbltelephone" runat="server" Font-Bold="true"></asp:Label>
</td>
<tr>
<td align="right">Other Telephone numbers:
</td>
<td>
<asp:Label ID="lblothertelephone" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td align="right">Street:
</td>
<td>
<asp:Label ID="lblstreet" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td align="right">P.O. Box:
</td>
<td>
<asp:Label ID="lblpoffice" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td align="right">City:
</td>
<td>
<asp:Label ID="lblcity" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td align="right">Country/region:
</td>
<td>
<asp:Label ID="lblcountry" runat="server" Font-Bold="true"></asp:Label>
</td>
<tr>
<td align="right">Account disabled?:
</td>
<td>
<asp:Label ID="lblaccdisabled" runat="server" Font-Bold="true"></asp:Label>
</td>
<tr>
<td align="right">Department:
</td>
<td>
<asp:Label ID="lbldepartment" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td align="right">Company:
</td>
<td>
<asp:Label ID="lblcompany" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td align="right">Manager:
</td>
<td>
<asp:Label ID="lblmgr" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
<tr>
<td align="right">Mobile:
</td>
<td>
<asp:Label ID="lblmobile" runat="server" Font-Bold="true"></asp:Label>
</td>
</tr>
</tr>
</tr>
</tr>
</table>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.DirectoryServices;
using System.Runtime.InteropServices;
namespace aspdotnet1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
//string connection = ConfigurationManager.ConnectionStrings["ADConnection"].ToString();
//DirectorySearcher dssearch = new DirectorySearcher(connection);
//dssearch.Filter = string.Format("(&(SAMAccountName={0}))", txtusername.Text);
//dssearch.Filter = "(sAMAccountName=" + txtusername.Text + ")";
var directoryEntry = new DirectoryEntry("LDAP://Domain name");
var directorySearcher = new DirectorySearcher(directoryEntry);
directorySearcher.Filter = string.Format("(&(SAMAccountName={0}))", txtusername.Text);
SearchResult sresult = directorySearcher.FindOne();
DirectoryEntry dsresult = sresult.GetDirectoryEntry();
try {
lblfname.Text = dsresult.Properties["givenName"][0].ToString();
lbllname.Text = dsresult.Properties["sn"][0].ToString();
lblemail.Text = dsresult.Properties["mail"][0].ToString();
//lbldisplayname.Text = dsresult.Properties["displayName"][0].ToString();
//if (dsresult.Properties["description"][5].ToString()=="")
//{
// lbldescription.Text = null;
//}
//lblofice.Text = dsresult.Properties["physicalDeliveryOfficeName"][0].ToString();
//lbltelephone.Text = dsresult.Properties["telephoneNumber"][0].ToString();
//lblothertelephone.Text = dsresult.Properties["otherTelephone"][0].ToString();
//lblstreet.Text = dsresult.Properties["streetAddress"][0].ToString();
//lblpoffice.Text = dsresult.Properties["postOfficeBox"][0].ToString();
//lblcity.Text = dsresult.Properties["l"][0].ToString();
//lblcountry.Text = dsresult.Properties["c"][0].ToString();
lblaccdisabled.Text = dsresult.Properties["userAccountControl"][0].ToString();
lbldepartment.Text = dsresult.Properties["department"][0].ToString();
lblcompany.Text = dsresult.Properties["company"][0].ToString();
lblmgr.Text = dsresult.Properties["manager"][0].ToString();
//lblmobile.Text = dsresult.Properties["mobile"][0].ToString();
//lblemail.Text = dsresult.Properties["mail"][0].ToString();
}
catch(Exception ex)
{
Response.Write(ex);
}
}
}
}
Thats it . you can change it as per your requirement. Enjoy coading
Comments