I have developed a software in C#, in which registration is done using mac id of PC as unique identifier. I registered the software on one PC using internet via usb tethering of my mobile, it took a mac id and got registered successfully. But when I tried to register on another PC using my mobile's internet via usb tethering, it is showing the same mac id which I got on registering first PC. I checked mac id of both PCs using ipconfig/all command in cmd and there same id is shown under physical address when my mobile is connected for internet. Why is this happening? What is the solution for this?
Code I used to fetch mac id:
string macAddresses = "", FinalmacAddresses = "";
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
macAddresses = nic.GetPhysicalAddress().ToString();
break;
}
FinalmacAddresses = macAddresses.Trim().ToString();
return FinalmacAddresses;
First thing is, you should never use a mac address as a unique identifier. There are tools and even simple ways by which you can assign multiple devices with the same mac address (Mac address spoofing). That is also not good, if we look through the security perspective.
I would recommend using a GUID instead.
Related
I am currently working on a C#-UWP app that needs to be able to discovery bluetooth devices (Not BLE) on the network and ones that have been previously connected to/paired.
Im sure anyone who is new to this task will have quickly found the documentation and example are of little help. I have learned more from Stackoverflow questions about peoples experimentations than from the docs and examples, but anyways.
My main question/problem is this: After setting up the device watcher to find bluetooth devices I found that I consistently get multiple additions of the same device but having a different bluetooth address (this is a device that was previously paired but not live on the network). After much investigate and brainstorming, we discovered that each device id is actually a pairing of the devices MAC address and the BT receivers MAC address.
The reason I was getting 3 device additions per 1 physical device is because I have connected to that same device with 3 different BT receiver dongles in the past. So my question is, is there anyway to make the device watcher return the device that corresponds to the currently active BT receiver dongle?
Otherwise I will need to find the currently active BT receivers MAC address and filter out the devices that do not have this, because otherwise the user will see 3 identical devices to select and only 1 of them will pass while the other 2 will fail.
While on this subject I would also like to mention that the device properties dont seem to be working. Before creating the watcher, I have a list of properties like this for example:
requestedProperties.Add("System.Devices.Aep.DeviceAddress");
requestedProperties.Add("System.Devices.Aep.IsConnected");
requestedProperties.Add("System.Devices.Aep.Bluetooth.Le.IsConnectable");
requestedProperties.Add("System.Devices.Aep.IsPresent");
requestedProperties.Add("System.Devices.Aep.ContainerId");
requestedProperties.Add("System.Devices.Aep.ModelId");
requestedProperties.Add("System.Devices.Aep.Manufacturer");
requestedProperties.Add("System.Devices.Aep.ProtocolId");
requestedProperties.Add("System.Devices.Aep.SignalStrength");
But when I debug the device that is added, it doesnt have any properties:
debug info showing no properties for added device
I would be useful to have this information.
Thank you for any input or suggestions.
Update
I found a quick solution that overcomes this problem (although i did find a lot more problems with the device watcher but that is probably a topic for another question).
For now I simply get the current BT adaptors MAC address and then check each incoming device if it has this MAC address in its pair. If it does not that means the device is paired with an old/unused BT adaptor.
/* NOTE:
Windows allows only 1 BT adapter to be active on 1 machine at a time
Therefore this function will either return the active dongle or a null if
there is no BT adapter on the machine or its disabled.
*/
BluetoothAdapter BTAdaptor = await BluetoothAdapter.GetDefaultAsync();
if (BTAdaptor == null)
{
// Log error
// return
}
//
// Code block to check if the BT adaptor can support the BT tech stack you are interested in.
//
// Format into hex with 12 characters (12 being the number of characters for MAC address)
string tempMac = BTAdaptor.BluetoothAddress.ToString("X12").ToLower();
// Pattern for MAC address.
string pattern = "(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})";
string replace = "$1:$2:$3:$4:$5:$6";
m_strBTAdapterMAC = Regex.Replace(tempMac, pattern, replace);
Then when the device is added/updated/removed by the watcher event, check it:
// If device is not paired with the currently used BT adapter.
if (deviceInfo.Id.Contains(m_strBTAdapterMAC) == false)
{
// Device paired with old dongle, dont want to show the user.
return;
}
If anyone ever figures out how to make the device watcher just not give old devices, please let me know, its probably a better solution.
I need to read a machine's MAC id in C# which stays fixed irrespective of the connection types e.g. connected to Work through network cable, wifi, VPN through Home wifi, through a Dongle or even can be offline.
So basically it need not necessarily be the MAC id of network interface which is "Up", i just need the MAC id which is/stays constant.
With the help of answers in the below link and other suggestions,
Reliable method to get machine's MAC address in C#
I am able to read the MAC ids, but still don't understand which one to consider that will reliably give me the fixed MAC id, which i can use in my application for some sort of verification in that particular system.
Here are the details of all network interfaces of that system, when the system is connected to "Work" through VPN using Home Wifi and connected to "Work" network directly:
Please suggest which one i should consider and what should be the right condition to filter out the interface with fixed MAC id in C#.
As indicated as "Related" to my question and also as pointed by #Caius Jard, i got the solution from the below link which will suit for my need:
How to determine MAC Address of the actual physical network card -- not virtual network interfaces created by VPN's (.NET C#)
because i want to:
-> take the MAC id of the physical card (i.e. excluding all virtual, pseudo, logical, usb) and don't allow my application without any physical card
-> consider the 1st card, in case more than 1 cards are installed
I have modified the query as per my need with the help of details given in below link:
https://weblogs.sqlteam.com/mladenp/2010/11/04/find-only-physical-network-adapters-with-wmi-win32_networkadapter-class/
So, my final solution looks like below:
ManagementObjectSearcher searcher = new ManagementObjectSearcher
(#"SELECT Index,MACAddress,PNPDeviceID
FROM Win32_NetworkAdapter
WHERE MACAddress IS NOT NULL AND PNPDeviceID IS NOT NULL AND Manufacturer != 'Microsoft' AND PNPDeviceID LIKE '%PCI\\%'");
IList<ManagementObject> mObject = searcher.Get()
.Cast<ManagementObject>()
.OrderBy(p => Convert.ToUInt32(p.Properties["Index"].Value))
.ToList();
foreach (var obj in mObject)
{
var mac = obj["MACAddress"].ToString();
mac = mac.Replace(":", string.Empty);
return mac;
}
Your physical (vs. virtual, logical) interface MAC addresses should stay same (at least until the physical device is replaced). Your "Ethernet 3", for example.
I need to get a unique ID from a computer using c#. I have done it with Mac Address by using the code
string macAddresses = "";
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
macAddresses += nic.GetPhysicalAddress().ToString();
break;
}
}
return macAddresses;
But when I run code in server , it returns Mac address of the server. How can I get the Mac address or any unique ID of the client system using c#.
There is nothing like a unique ID that is automatically sent to the server with requests.
One way to implement this is to generate a unique ID on the server for each time a new client connects and send this ID to the client so that it can send it with each subsequent request and you can identify it by this ID.
Of course you have to make sure that the ID you generate is actually unique and you have to store the generated IDs on the server to know about existing "connections" and to be able to reject fake IDs.
As Martin said:
There is nothing like a unique ID that is automatically sent to the server with requests.
.
You need to get that information passed INSIDE your request to be able to process it in the C# code.
For security reasons, by default of all modern broswers, you can't access the client's MAC address via JS. See more
(ASP.Net) You could set up a Session GUID as a cookie at the first time the client access your website, that should be enought to identify the users.
(WinForms/WPF) You can use that same code at the CLIENT application, then send it to your api/webservice.
(WinForms/WPF) There is also something called HD UUID (Maybe Windows-only), which can be modified by third-party softwares, which makes it not 100% safe (I've seen people modifying this to "fake auth" some licensed softwares). See more
Also could be useful if you specify what kind of server is this, I'm assuming it is web, but got some WinForms/WPF alternatives
I am creating a UWP application, and I am trying to find all devices connected to my LAN at home. I have a pi running windows iot that is a controller and I have another application running on my computer currently but will be running on another device later. What I need to do is find what address all pi's are and allow the user to select the one they want. I have started by just finding all things connected to my local network.
List<string> connectedDevices = new List<string>();
foreach (HostName localHostName in NetworkInformation.GetHostNames())
{
if (localHostName.IPInformation != null)
{
if (localHostName.Type == HostNameType.Ipv4)
{
connectedDevices.Add(localHostName.ToString());
}
}
}
ConnectedDevices = connectedDevices;
This code will return some ip addresses but not the one the pi is connected to. I have verified that the pi is in fact connected by pining it and also I am able to control it when entering the ip address in my application myself.
What I am trying to accomplish is taking the master device with the display and finding the slave devices on the local network so multiple slaves can be added and if an ip address of a slave has changed it will be updated so my app can still call it.
Any help would be appreciated.
I am currently working on a software solution written in C# .NET 4.5. The software uses a licensing system that is based on hardware IDs (for example MAC address or CPU ID).
One user now reported that he has issues with the licensing when using the software with Microsoft App-V. He mentioned that every time a new User wants to use the software the application complains that the license is not valid (due to a change in the hardware).
This also happens if a previously registered user uses the Software on a different client.
My question now is, when running an application via App-V, what does the following snipped of code return, the Mac address of the client or of the server where to application is actually running. If the first is true, is there a way to get the same information from the server too, using some functionality in .NET?
private static string getMAC() {
ManagementClass oMClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection MOCol = oMClass.GetInstances();
string mac = "";
foreach (ManagementObject MO in MOCol) {
if (MO != null) {
if (MO["MacAddress"] != null) {
mac = MO["MacAddress"].ToString().Replace(":", "");
if (mac != string.Empty) {
break;
}
}
}
return mac;
}
Next-to-last bullet in the Limitations section in App-V's Wikipedia article fits your problem exactly:
Licensing Policies: Applications with licensing enforcement tied to the machine, e.g. the license is tied to the system’s MAC address or harddisk serial number. This type of application should not be sequenced if the activation can not be done by the user at the first launch of sequenced application, manually or by script.
You'll need to tell your customer that you cannot support App-V if you verify the license on each individual run of the app instead of just once at app install time. If that means that you'll lose a valuable customer then quickly get rid of this scheme, a business decision we cannot make for you.