How to get mac address of client in asp.net? - c#

i want get mac address of my website visitor in asp.net
how can i do it?
this code get host mac address
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if (!(bool)objMO["ipEnabled"])
continue;
string a = ((string)objMO["MACAddress"]);
}

You cannot get the MAC of your visitor because MAC addresses do not survive hops across different hosts like IP addresses do. At the most what you could get is the MAC of the switch closest to the server, and even in that case I 'm not sure it is technically possible from ASP.NET.

Related

the query Win32_NetworkAdapterConfiguration return HRESULT: 0x80070422

I have below code to get the physical mac address from a machine, it works fine in most but in some I get an error
like that (the orginal error message isn't English, so I'm trying to translate it myself):
the service couldn't be started because it's disable or doesn't have
an associated active devices (exception HRESULT: 0x80070422)
What's that service that couldn't be started or is disable? how do I fix this?
Here's the code I'm using:
public string GetMACAddress()
{
ManagementObjectSearcher objMOS = new ManagementObjectSearcher("Select * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMOS.Get();
string macAddress = String.Empty;
foreach (ManagementObject objMO in objMOC)
{
object tempMacAddrObj = objMO["MacAddress"];
if (tempMacAddrObj == null) //Skip objects without a MACAddress
{
continue;
}
if (macAddress == String.Empty) // only return MAC Address from first card that has a MAC Address
{
macAddress = tempMacAddrObj.ToString();
}
objMO.Dispose();
}
return macAddress;
}
It looks like Windows Management Instrumentation service is down/disabled.
Click Start
Click Run
Type services.msc and press . This will open the Services
window
Scroll down to Windows Management Instrumentation service.
Right click on the service
Verify that the service is started and set to Automatic.
Click OK

Get MAC Address using ManagementObjectSearcher

I am trying to develop a registration algorithm in C#. I used MAC address of the client machine to generate the request code. The function is shown below. But in Windows 7, This function shows a NullRererenceException in this line.
mac = mo["MACAddress"].ToString();
public string GetMACAddress()
{
string mac = null;
ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration");
foreach (ManagementObject mo in mos.Get())
{
mac = mo["MACAddress"].ToString();
break;
}
return mac;
}
What is the most reliable way to get MAC address, in Windows 7 and Windows 8, using C#, in order to develop an activation algorithm?
Not all object content the MAC address so need to check which one dose have the MAC
you can do some thing like this
string macAddress = String.Empty;
foreach (ManagementObject mo in mos.Get())
{
object tempMacAddrObj = MO["MacAddress"];
if (tempMacAddrObj == null) //Skip objects without a MACAddress
{
continue;
}
if (macAddress == String.Empty) // only return MAC Address from first card that has a MAC Address
{
macAddress = tempMacAddrObj.ToString();
}
objMO.Dispose();
}
For the purpose of license activation I would actually recommend to use something else (or in addition) to the MAC address, as this is easily spoofed. Here is a very nice C# tutorial on how to obtain a "hardware fingerprint" that should solve your problem: http://www.codeproject.com/Articles/28678/Generating-Unique-Key-Finger-Print-for-a-Computer

Get IP address from hostname in LAN

I found many samples on how to get a hostname by an IP address, how can I get the IP address of a host in the LAN?
Try this
public static void DoGetHostAddresses(string hostname)
{
IPAddress[] ips;
ips = Dns.GetHostAddresses(hostname);
Console.WriteLine("GetHostAddresses({0}) returns:", hostname);
foreach (IPAddress ip in ips)
{
Console.WriteLine(" {0}", ip);
}
}
i got this from http://msdn.microsoft.com/en-us/library/system.net.dns.gethostaddresses.aspx
Here is simple code if you want to get the IP Address(V4) from your pc.
Import this library into your class
using System.Net;
Initialize and declare these variables into your codes. They contain hostname, ipaddress and an array of Host Addresses:
string HostName = Dns.GetHostName().ToString();
IPAddress[] IpInHostAddress = Dns.GetHostAddresses(HostName);
string IPV4Address = IpInHostAddress[1].ToString(); //Default IPV4Address. This might be the ip address you need to retrieve
string IPV6Address = IpInHostAddress[0].ToString(); //Default Link local IPv6 Address
Open your command prompt, just type "ipconfig" and press enter.Once you are done, you could check if the string IPV4Address matches to IPv4Address in our pc.
As long as you know the name of a machine, you can use Dns.GetHostAddresses. Your network DNS should recognize it as LAN computer and return proper IP.
Use Dns.GetHostEntry(hostname) instead of obsolete Dns.GetHostAddresses.
Here is an excellent example of how it is doing: http://www.codeproject.com/Articles/854/How-To-Get-IP-Address-Of-A-Machine
you could use the windows management classes to do this, it also works for remote machines that are in the same domain (but I don't know if they need to enable or disable any security or policy settings for this to work). for example:
public List<NetworkAdapter> GetAdapterList()
{
ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration ");
ManagementObjectCollection moc = mgmt.GetInstances();
List<NetworkAdapter> adapters = new List<NetworkAdapter>();
// Search for adapters with IP addresses
foreach(ManagementObject mob in moc)
{
string[] addresses = (string[])mob.Properties["IPAddress"].Value;
if (null == addresses)
{
continue;
}
NetworkAdapter na = new NetworkAdapter();
na.Description = (string) mob.Properties["Description"].Value;
na.MacAddress = (string) mob.Properties["MACAddress"].Value;
na.IPAddresses = addresses;
adapters.Add(na);
}
return adapters;
}
and to access a remote machine create the management class like this instead:
ManagementClass mgmt = new ManagementClass
(\\\\servername\\root\\cimv2:Win32_NetworkAdapterConfiguration);
this approach may get you more IPs than just the ones that have been registered in the DNS.

How can service know the caller?

I have a WCF service.
How can i know if the call to my service comes from local machine or a machine from network?
Thanks,
Adrya
You could check the IP of the caller. If it is from the local machine should be "127.0.0.1".
You can get the IP of the caller (the remote address) from the OperationContext object.
More info here:
http://www.danrigsby.com/blog/index.php/2008/05/21/get-the-clients-address-in-wcf/
I'd compile a list at startup of ALL the known IP addresses on the local machine using something like....
NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
List<string> addressList = new List<string>();
foreach (NetworkInterface ni in nis)
{
IPInterfaceProperties iip = ni.GetIPProperties();
UnicastIPAddressInformationCollection unis = iip.UnicastAddresses;
foreach (UnicastIPAddressInformation uni in unis)
{
string address = uni.Address.ToString();
addressList.Add(address);
}
}
and then check the addressList to see if it contains the 'remote' IP address.
That should cover any request presenting itself from the local machine with an IP addy other than 127.0.0.1.

Get MAC Address when network adapter is disabled?

Is there any way i can retrieve MAC Address when Network Adapter is disabled in .net?
Thanks in advance,
It is not possible to get the MAC address of an adapter which is disabled: this is because getting the MAC address requires querying the driver, and the driver for a disabled adapter is not loaded (source).
You can, however get the MAC address of an adapter which is not currently connected.
The WMI route is no good here, because it shows the MAC address as null for adapters which are not connected. The good news is that the NetworkInterface.GetAllNetworkInterfaces() route works just fine:
// using System.Net.NetworkInformation;
var nics = NetworkInterface.GetAllNetworkInterfaces();
// pick your NIC!
var selectedNic = nics.First();
var macAddress = selectedNic.GetPhysicalAddress().ToString();
Refer this link.
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.physicaladdress.aspx
The example here displays physical address of all interface irrespective of their operational stage. HTH.
You can use WMI:
public static string GetMACAddress()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
string MACAddress=String.Empty;
foreach(ManagementObject mo in moc)
{
if(MACAddress==String.Empty) // only return MAC Address from first card
{
MACAddress= mo["MacAddress"].ToString() ;
}
mo.Dispose();
}
return MACAddress;
}
Using MS PowerShell command get-NetAdapter one can get the disabled network adapter's MAC Address.
More info at get-NetAdapter

Categories

Resources