Get All Intranet User Details in ASP.Net

Introduction

This article describes how to get the details of users present in an intranet. Here I will get the information from the Win32_UserAccount class.

What is Win32_UserAccount

The Win32_UserAccount WMI class contains information about a user account on a computer system running Windows.

Design

Create a new ASP.Net Empty website.

Add one page in that website.

In that page drag and drop a GridView control.

Design your screen as in the following screen.

1.png

Or you can copy the following source code:
  1. <body>  
  2.     <form id="form1" runat="server">  
  3.     <div>  
  4.         <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" >  
  5.             <AlternatingRowStyle BackColor="White" />  
  6.             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
  7.             <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />  
  8.             <RowStyle BackColor="#EFF3FB" />  
  9.         </asp:GridView>  
  10.     </div>  
  11.     </form>  
  12. </body>
Next add a reference of System.Management.

To add the reference use the following procedure.

Go to Solution Explorer, select the project and right-click on that and choose "Add Reference" from the menu.

2.png

A window will open; in that choose the .Net tab.

3.jpeg

It will show  a  list. In that list, choose System.Management and click the "OK" button.

4.jpeg

Now go to code view and write following code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Management;  
  8. using System.Data;  
  9. public partial class _Default : System.Web.UI.Page  
  10. {  
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.         DataTable dt = new DataTable();  
  14.         dt.Columns.Add(new DataColumn("User name"typeof(string)));  
  15.         dt.Columns.Add(new DataColumn("Account Type"typeof(string)));  
  16.         dt.Columns.Add(new DataColumn("Caption"typeof(string)));  
  17.         dt.Columns.Add(new DataColumn("Description"typeof(string)));  
  18.         dt.Columns.Add(new DataColumn("Disabled"typeof(string)));  
  19.         dt.Columns.Add(new DataColumn("Domain"typeof(string)));  
  20.         dt.Columns.Add(new DataColumn("Instal Date"typeof(string)));  
  21.         dt.Columns.Add(new DataColumn("Local Account"typeof(string)));  
  22.         dt.Columns.Add(new DataColumn("Lockout"typeof(string)));  
  23.         dt.Columns.Add(new DataColumn("Password Changeable"typeof(string)));  
  24.         dt.Columns.Add(new DataColumn("Password Expires"typeof(string)));  
  25.         dt.Columns.Add(new DataColumn("Password Required"typeof(string)));  
  26.         dt.Columns.Add(new DataColumn("SID"typeof(string)));  
  27.         dt.Columns.Add(new DataColumn("SID Type"typeof(string)));  
  28.         dt.Columns.Add(new DataColumn("Status"typeof(string)));  
  29.         SelectQuery Sq = new SelectQuery("Win32_UserAccount");  
  30.         ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher(Sq);  
  31.         ManagementObjectCollection osDetailsCollection = objOSDetails.Get();  
  32.         foreach (ManagementObject MO in osDetailsCollection)  
  33.         {  
  34.             DataRow dr = dt.NewRow();  
  35.             dr["User name"] = MO["FullName"].ToString();  
  36.             dr["Account Type"] = MO["AccountType"].ToString();  
  37.             dr["Caption"] = MO["Caption"];  
  38.             dr["Description"] = MO["Description"];  
  39.             dr["Disabled"] = MO["Disabled"];  
  40.             dr["Domain"] = MO["Domain"];  
  41.             dr["Instal Date"] = MO["InstallDate"];  
  42.             dr["Local Account"] = MO["LocalAccount"];  
  43.             dr["Lockout"] = MO["Lockout"];  
  44.             dr["Password Changeable"] = MO["PasswordChangeable"];  
  45.             dr["Password Expires"] = MO["PasswordExpires"];  
  46.             dr["Password Required"] = MO["PasswordRequired"];  
  47.             dr["SID"] = MO["SID"];  
  48.             dr["SID Type"] = MO["SIDType"];  
  49.             dr["Status"] = MO["Status"];  
  50.             dt.Rows.Add(dr);  
  51.         }  
  52.         GridView1.DataSource = dt;  
  53.         GridView1.DataBind();  
  54.     }  
  55. }
In the code above I get the information from Win32_UserAccount and store it in a datatable and bind it to the grid.

SelectQuery

It Represents a WQL (WMI Query Language) SELECT data query.

ManagementObjectSearcher

It retrieves a collection of management objects based on a specified query.

This class is one of the more commonly used entry points to retrieving management information.

ManagementObjectCollection

It represents various collections of management objects retrieved using WMI.

Now build your application, it will show all intranet userinfo in the grid.

5.jpg


Similar Articles