Subscribe For Free Updates!

We'll not spam mate! We promise.

Thursday, 17 October 2013

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:


<
body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" >
            <AlternatingRowStyle BackColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
        </asp:GridView>
    </div>
    </form>

</
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:


using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Management;
using
System.Data;
public
partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("User name", typeof(string)));
        dt.Columns.Add(new DataColumn("Account Type", typeof(string)));
        dt.Columns.Add(new DataColumn("Caption", typeof(string)));
        dt.Columns.Add(new DataColumn("Description", typeof(string)));
        dt.Columns.Add(new DataColumn("Disabled", typeof(string)));
        dt.Columns.Add(new DataColumn("Domain", typeof(string)));
        dt.Columns.Add(new DataColumn("Instal Date", typeof(string)));
        dt.Columns.Add(new DataColumn("Local Account", typeof(string)));
        dt.Columns.Add(new DataColumn("Lockout", typeof(string)));
        dt.Columns.Add(new DataColumn("Password Changeable", typeof(string)));
        dt.Columns.Add(new DataColumn("Password Expires", typeof(string)));
        dt.Columns.Add(new DataColumn("Password Required", typeof(string)));
        dt.Columns.Add(new DataColumn("SID", typeof(string)));
        dt.Columns.Add(new DataColumn("SID Type", typeof(string)));
        dt.Columns.Add(new DataColumn("Status", typeof(string)));
        SelectQuery Sq = new SelectQuery("Win32_UserAccount");
        ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher(Sq);
        ManagementObjectCollection osDetailsCollection = objOSDetails.Get();
        foreach (ManagementObject MO in osDetailsCollection)
        {
            DataRow dr = dt.NewRow();
            dr["User name"] = MO["FullName"].ToString();
            dr["Account Type"] = MO["AccountType"].ToString();
            dr["Caption"] = MO["Caption"];
            dr["Description"] = MO["Description"];
            dr["Disabled"] = MO["Disabled"];
            dr["Domain"] = MO["Domain"];
            dr["Instal Date"] = MO["InstallDate"];
            dr["Local Account"] = MO["LocalAccount"];
            dr["Lockout"] = MO["Lockout"];
            dr["Password Changeable"] = MO["PasswordChangeable"];
            dr["Password Expires"] = MO["PasswordExpires"];
            dr["Password Required"] = MO["PasswordRequired"];
            dr["SID"] = MO["SID"];
            dr["SID Type"] = MO["SIDType"];
            dr["Status"] = MO["Status"];
            dt.Rows.Add(dr);
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
In the code above I get the information from Win32_UserAccount and store it in a datatable and bind it to the grid.

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

ManagementObjectSearcherIt 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.

ManagementObjectCollectionIt represents various collections of management objects retrieved using WMI.

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



5.jpg 

To download the source click here

Socializer Widget By Blogger Yard
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment