Getting MAC Address of Client Machine in C #

Usually MAC address is also used to keep track of the unique Identification, if IP Address are not static, Every then from same system it will generate different IP Address, so cannot be keep records for unique, unless it is Static IP.

But MAC is set in users hardware system it is easy to trace, it is also used in E-commerce website, even IRCTC uses MAC Address to traces the records to trace the number of new user per day.

A media access control address (MAC address), also called physical address, is a unique identifier assigned to network interfaces for communications on the physical network segment.

MAC addresses are used as a network address for most IEEE 802 network technologies, including Ethernet and WiFi. Logically, MAC addresses are used in the media access control protocol sublayer of the OSI reference model.
MAC addresses are most often assigned by the manufacturer of a network interface controller (NIC) and are stored in its hardware, such as the card’s read-only memory or some other firmware mechanism.

If assigned by the manufacturer, a MAC address usually encodes the manufacturer’s registered identification number and may be referred to as the burned-in address (BIA). It may also be known as an Ethernet hardware address (EHA), hardware address or physical address. This can be contrasted to a programmed address, where the host device issues commands to the NIC to use an arbitrary address.

How do I find out the MAC address of my computer? (Windows)
• Click the Start icon
• Click Control Panel
• Select Network and Internet
• Click Network and Sharing Centre
• Select Change Adapter Settings from the list on the left hand side
• You may have a separate icon for your Wireless connection, Ethernet (Wired) and a Virtual WiFi MiniPort. Each has its own MAC address.
• Right click on the connection you are trying to find the MAC address for and select Properties
• Hover your mouse over the text box underneath connect using . Your MAC address will appear in the tooltip and is made up of 12 characters e.g. 00:11:22:AA:66:DD
using System.Management;
using System.IO;

protected void Page_Load(object sender, EventArgs e)
string MacAddress = GetMACAddress();

public string GetMACAddress()
string macAddresses = “”;

foreach (System.Net.NetworkInformation.NetworkInterface nic in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
if (nic.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up)
macAddresses += nic.GetPhysicalAddress().ToString();
return macAddresses;

Authentication using LDAP (Active Directory) for ASP.NET applications

Authentication: The process of identifying an individual, usually based on a username and password.

Generally every applications needs user authentication and we have few ways

(Forms Authentication, Windows Authentication & Passport Authentication)

to authenticate the users in web applications. Most of the intranet web applications authenticate the users against active directory by using windows user name and passwords.

The main benefit of this LDAP (lightweight directory access protocol) authentication is application users do not maintain separate user name and password for each application. Users can able to use their windows user name and password for all application.

Namespace details:

System.DirectoryServices library plays main role on this functionality, this takes the user’s user name and password and validating against windows active directory.


using System.DirectoryServices;

String adPath = “LDAP://”;

String DomainName = “uzval”;

public bool IsAuthenticated(string adPath, string domain, string username, string pwd)


string wholeString = username;

string firstBit = wholeString.Split(‘@’)[0];

username = firstBit;

string domainAndUsername = domain + @”\” + username;

DirectoryEntry entry = new DirectoryEntry(adPath, domainAndUsername, pwd);



//Bind to the native AdsObject to force authentication.

object obj = entry.NativeObject;

DirectorySearcher search = new DirectorySearcher(entry);

search.Filter = “(SAMAccountName=” + username + “)”;



//CN or Display Name




SearchResult result = search.FindOne();

if (null == result)


return false;




Session[“ADUserID”] = string.Empty;

Session[“ADUserName”] = string.Empty;

Session[“ADuserAccountControl”] = string.Empty;

//ADUser UserId

Session[“ADUserID”] = result.Properties[“SAMAccountName”][0];

//AD UserName

Session[“ADUserName”] = result.Properties[“cn”][0];

                Session[“ADuserAccountControl”] = Convert.ToString(result.Properties[“userAccountControl”][0]);

//User Account Control values

//Allow all these ID’s to login- 512,544,4096,66048,590336,532480

//512 – Enable Account

//514 – Disable account

//544 – Account Enabled – Require user to change password at first logon

//4096 – Workstation/server

//66048 – Enabled, password never expires

//590336 – Enabled, User Cannot Change Password, Password Never Expires

//66050 – Disabled, password never expires

//262656 – Smart Card Logon Required

//532480 – Domain controller



catch (Exception ex)


return false;

//throw new Exception(“Error authenticating user. ” + ex.Message);


return true;


Windows Services

Windows Service:

Microsoft Windows services, formerly known as NT services, enable you to create long-running executable applications. These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface. These features make services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer.

Create a Windows Service

Creating Windows Service is very easy with visual studio just follow the below steps to create windows service

Open visual studio –> Select File –> New –>Project–> select Windows Service

And give name as MyWindowsService after that rename Service1.cs to SchedulingService.cs


 After change name of service, open SchedulingService.cs in design view right click and select Properties now one window will open in that change Name value to SchedulingService and change ServiceName to SchedulingService. Check below properties window that should be like this


 also in design view of SchedulingService.cs right click and select Add Installer files to our application. Now you will see designer screen added to project with 2 controls: serviceProcessInstaller1 and ServiceInstaller1

 Main purpose of using Windows Installer is an installation and configuration service provided with Windows.

 Now right click on serviceProcessInstaller1 and select properties in that change Account to LocalSystem


After completion of setting all the properties now we need to write the code to run the windows services at scheduled intervals.

To add code to Windows Service

Right Click on SchedulingService.cs [Design] and select View Code, then you will see SchedulingService.cs

Now write below code,

publicpartialclassSchedulingService : ServiceBase


//Initialize the timer

Timer timer = newTimer();

public SchedulingService()




 protectedoverridevoid OnStart(string[] args)


//add this line to text file during start of service

TracingService(“Start Service”);

//handle Elapsed event

timer.Elapsed += newElapsedEventHandler(OnElapsedTime);

 //This statement is used to set interval to 1 minute (= 60,000 milliseconds)

 timer.Interval = 60000;

 //enabling the timer

timer.Enabled = true;


protectedoverridevoid OnStop()


timer.Enabled = false;

TracingService(“Stopping Service”);


 privatevoid OnElapsedTime(object source, ElapsedEventArgs e)


TracingService(“Entry at “ + DateTime.Now);


privatevoid TracingService(string content)


//set up a filestream

FileStream fs = newFileStream(@”d:\ScheduledService.txt”, FileMode.OpenOrCreate, FileAccess.Write);

 //set up a streamwriter for adding text

StreamWriter sw = newStreamWriter(fs);

 //find the end of the underlying filestream

sw.BaseStream.Seek(0, SeekOrigin.End);

 //adding the text



//close the writer




Now Build the application, which generates .EXE file.

How to Instal Windows Service ?

To install windows service in your system follow these steps

Start –>All Programs –>Microsoft Visual Studio 2008 –>Visual Studio Tools –>Open Visual Studio Command Prompt

Go to the path where .EXE generated.

Now type, installutil MyWindowsService.EXE

with this service was installed successfully.

Now Installed service will be started with below steps,

Start –>Control Panel –>Open Control Panel –>Select Administrative Tools–>Computer Management –>Services and Applications –>Services –>Open services

check for your service name and right click on that and select Start option.

Now You windows service is installed and started working.

How to stop or uninstall windows service ?

Installed service will be stopped with below steps,

Start –>Control Panel –>Open Control Panel –>Select Administrative Tools–>Computer Management –>Services and Applications –>Services –>Open services

check for your service name and right click on that and select Stop option.

If we want to uninstall the installed windows service you just point to your service same as what I explained previously and type statement installutil /u and your service name Ex : Installutil /u MyWindowsService.EXE

Restricting access to local drive in MyComputer

Some times it is necessary to restrict access to local drive to some users, hence it can be achieved by following below steps…

1. Open My Computer

2. Click on Tools, Folder Options

3. Click on the view tab

4. Scroll down the list of advanced settings until you get to an option called use simple file sharing. Click to uncheck that box, then click on OK.

5. Right-click on the drive you want to limit access to, then click on properties

6. Click on the security tab.

7. Remove everyone from the group or user names list that you don’t want to access the drive.

Click apply and Ok.

Then the users present in user list can only access that drive.

Be careful with security settings…