I am trying to get the MAC address of the client pc but it shows the mac address of the IIS server where my project is hosted.
protected void Page_Load(object sender, EventArgs e)
{
NetworkInterface[] anics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in anics)
{
if (amacaddress == String.Empty)
{
IPInterfaceProperties properties = adapter.GetIPProperties();
amacaddress = adapter.GetPhysicalAddress().ToString();
lblname.Visible = true;
string ip = Request.UserHostAddress;
lblname.Text = "MAC Address is :- " + amacaddress + " "+ ip;
}
}
}
Yeah. That is similar to asking for getting the IMSI of a phone from a phone call - not possible, you call a phone number, the rest is implementation detail. MAC Addresses pretty much never travel more than one ethernet domain (next switch/router). They are not pat of the IP protocol layer. As such, you can not get them from the http request, which ultimatly is a TCP thus an IP connection. YOu will have to execute (C#, not javascript) code on the client to possibly get the local MAC AddressES - that is plural, there may be more than one (as in: 2 local network cards, a wireless adapter = 3 mac addresses).
Related
How do I get ipv4 of another device connected same wifi network?
I used below code (C# - Unity):
string ipv4 = "";
string hostName = "anp.local";
var host = Dns.GetHostEntry(hostName);
foreach (var ip in host.AddressList)
{
Debug.Log("IP: " + ip.ToString());
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
ipv4 = ip.ToString();
}
}
I got a wrong result: 125.235.4.59 on Window and Android.
Expect result should be 192.168.1.100 (work on Ipad)
My goal is able to connect another device in same wifi network on Window/Android/IOS platforms and I planed using ipv4 for it.
I have searched and read almost every article related to my search about how to get client ipaddress and machine name but all the codes return only server ip and name. I've tried these codes also given below, it gives my PC ip on localhost but on the server, it gives server ip and name :
string stringHostName = Dns.GetHostName();
IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);
and also used this method :
public string GetUserIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDER_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[0];
}enter code here
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}
It gives ::1 on localhost but on the server it gives server ip only.
Kindly help me if there any way possible to get client ipaddress and machine name using C#.
I'm trying to build C# app (desktop,windows, it doesn't really matter for now), with which I would like to connect to my windows phone, using sockets, to transfer some data... I know how this can be achieved,.
When connecting through sockets I don't want to manually enter windows phone device's IP address. So I want to know if it is possible to send some HTTP request from Windows phone app with some message, and fetch that message on computer, to be sure which IP is windows phone's IP?
In other words how to know which IP address belongs to Windows phone's IP address from Bunch of Ip addresses of devices on network?
Fallow this Link from Here
Add NameSpace :
using Windows.Networking;
public static string FindIPAddress()
{
List<string> ipAddresses = new List<string>();
var hostnames = NetworkInformation.GetHostNames();
foreach (var hn in hostnames)
{
//IanaInterfaceType == 71 => Wifi
//IanaInterfaceType == 6 => Ethernet (Emulator)
if (hn.IPInformation != null &&
(hn.IPInformation.NetworkAdapter.IanaInterfaceType == 71
|| hn.IPInformation.NetworkAdapter.IanaInterfaceType == 6))
{
string ipAddress = hn.DisplayName;
ipAddresses.Add(ipAddress);
}
}
if (ipAddresses.Count < 1)
{
return null;
}
else if (ipAddresses.Count == 1)
{
return ipAddresses[0];
}
else
{
//if multiple suitable address were found use the last one
//(regularly the external interface of an emulated device)
return ipAddresses[ipAddresses.Count - 1];
}
}
Edit:
If I am wrong You want to get particular Windows Phone IP address from bunch of IP addresses. AFAIK you can't get like this. But I suggest you to Append some string when getting IP of Windows Phone like below which will vary from other Ip addresses by looking it self.
string IPadd = FindIPAddress();
string WPIPadd = IPadd + " - Windows phone "
MessageDialog.show(WPIPadd);
If you find any thing new apart from this Let us know. Thanks
Is there a 1 line method to get the IP Address of the server?
Thanks
Request.ServerVariables["LOCAL_ADDR"];
From the docs:
Returns the server address on which the request came in. This is important on computers where there can be multiple IP addresses bound to the computer, and you want to find out which address the request used.
This is distinct from the Remote addresses which relate to the client machine.
From searching the net I found following code: (I couldn't find a single line method there)
string myHost = System.Net.Dns.GetHostName();
// Show the hostname
MessageBox.Show(myHost);
// Get the IP from the host name
string myIP = System.Net.Dns.GetHostEntry(myHost).AddressList[index].ToString();
// Show the IP
MessageBox.Show(myIP);
-> where index is the index of your ip address host (ie. network connection).
Code from: http://www.geekpedia.com/tutorial149_Get-the-IP-address-in-a-Windows-application.html
As other(s) have posted, System.Net.Dns.GetHostEntry is the way to go. When you access the AddressList property, you'll want to take the AddressFamily property into account, as it could return both IPv4 AND IPv6 results.
This method will return your machine public IP address when run this code on your PC and when you deploy your application on server will return Server IP address.
public static string Getpublicip()
{
try
{
string externalIP = "";
var request = (HttpWebRequest)WebRequest.Create("http://icanhazip.com.ipaddress.com/");
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
externalIP = new WebClient().DownloadString("http://icanhazip.com");
return externalIP;
}
catch (Exception e)
{
return "null";
}
}
Is there a way to get a list of all SSID's and their mac address of reachable signals in my area?
I tried the Nativ WlanApi in my c# code. What I get is the list of all ssid's, but for
getting their mac address, I don't have any idea.
This is the code I using for getting the list:
private void show_all_ssids_Click(object sender, EventArgs e)
{
WlanClient client = new WlanClient();
foreach ( WlanClient.WlanInterface wlanIface in client.Interfaces )
{
// Lists all available networks
Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList( 0 );
this.ssidList.Text = "";
foreach ( Wlan.WlanAvailableNetwork network in networks )
{
//Trace.WriteLine( GetStringForSSID(network.dot11Ssid));
this.ssidList.Text += GetStringForSSID(network.dot11Ssid) + "\r\n";
}
}
}
static string GetStringForSSID(Wlan.Dot11Ssid ssid)
{
return Encoding.ASCII.GetString(ssid.SSID, 0, (int)ssid.SSIDLength);
}
I hope there is a way.
In order to get a MAC address you would need to connect to that wireless network. Once you are connected you should be able to get the MAC address of machines on the immediate network using the same methods that you might for traditional wired networks - I believe that the best way of doing that would be by parsing the output of the arp -a command.
this is the solution:
Dim networksBss As Wlan.WlanBssEntry() = SelectedWifiAdapter.GetNetworkBssList()
For car = 0 To networksBss(i).dot11Bssid.Length - 1
If Len(Hex(networksBss(i).dot11Bssid(car))) = 1 Then ThisScan(i).MAC = ThisScan(i).MAC & "0"
ThisScan(i).MAC = ThisScan(i).MAC & Hex(networksBss(i).dot11Bssid(car)) & ":"
Next
anyway i'm still looking for a way to find details (strenght) of networks with SSID="" associating it with the proper MAC.