views:

764

answers:

4

In an ASP.NET (C#) WebApp, I can get the IP of the visitors' PC easily, But How to get the MAC address of the visitors' PC in an ASP.NET webApp?

And this ASP.NET app is runing on the inner intranet of our company, and the visitors are also in the same inner intranet of our company.

+5  A: 

The MAC address is not part of the IP header (or any other protocols above that), and thus not available if all you see is the HTTP traffic.

EDIT (after OP's update): Since clients and servers are on the same internal network, wouldn't it be better to get a host name from the IP address instead of the MAC address? You can easily look up the host name based on the IP address.

Brian Rasmussen
More specifically, the MAC address is not useful (as far as network communication goes) beyond the first router between you and them.
Matthew Scharley
Why the downvote?
Brian Rasmussen
@ -1: Because it *is* available, and it's a bit irrelevant to say it's not part of the IP header - since that's not where physical addressing lives. It's like saying what's in the CPU register is not part of the Windows API, thus it's not available....
Mark Brackett
@Mark Brackett: Before the update the only thing the OP stated was that this was a web application. I.e. the only available information is what you can get from IP and above in the TCP/IP stack and therefore the MAC address is not available in a regular web scenario. If you have information that states otherwise please do include a link.
Brian Rasmussen
@Brian - I *did* include a link to GetIpNetTable, which gives you the ARP table. Folks just seem to like your answer better. ;)
Mark Brackett
I did see your answer. However, the ARP table only holds info on local machines. The MAC address is used in the data link layer. This information is not transfered between different IP networks and thus it will not be available as part of regular web traffic. Given the fact that the OP's server and clients appear to be on the same local network it may be possible to map IP addresses to MAC addresses, but as I said that was not the original context of the question. I still fail to see how my answer is wrong and irrelevant as you claim.
Brian Rasmussen
@Brian "This information is not transfer[r]ed between different IP networks and thus it will not be available as part of regular web traffic", that much is true. But it has nothing to do with the fact that it's not in the IP header - the IP header is irrelevant for MAC addresses whether you're on the same subnet or not. In other words, the MAC address [of the client] *is [sometimes] available*, but *is never* a part of the IP header. In the case of a remote subnet, the MAC address is for the next hop - which is probably not what you want - but all that your server will get from the network.
Mark Brackett
All that being said - I also prefer "positive" responses that give solutions (like IanT8's about an ActiveX control or an ARP service) rather than "negative" responses that state that it's not possible. Nothing personal, I just found your wording a bit imprecise and don't think the answer that states "it can't be done" should be ahead of answers that actually show *how it can be done*.
Mark Brackett
@Mark: I am perfectly aware that the MAC address is never part of the IP header (I linked to the specification, albeit the wiki not the RFC). My point was that since this is the lowest level protocol a web application is guaranteed to see it should be obvious that the MAC is not available to a web application. The MAC will thus never be part of the traffic between a web server and a client. I acknowledge the fact, that the server may be able to retrieve the MAC address from the IP address for any local clients, but I fail to see how that in any way invalidates my point.
Brian Rasmussen
@Mark: I am sorry, but your answer does not show how it can be done. It shows that in some specific cases you can dig out the information yourself, but it cannot be considered a general solution to the OP's question. As for the ActiveX solution, while it is an option it has several obvious drawbacks and I would not call it a general solution either. In a nutshell MAC addresses are not part of the traffic between a web server and a client. That was my answer. I am sorry you don't like it, but I can't see how that changes the fact.
Brian Rasmussen
+1  A: 

You can't easily do this. There are protocols such as ARP which allow translation between MAC and IP addresses, but this traffic is typically behind a firewall and so not available to you on a public website.

On an intranet, you might be able to do something, but not via ASP.NET. You would need to use other mechanisms to capture this information - but those kinds of tools (e.g. packet sniffers) are generally not available to developers and may contravene corporate IS policies.

Vinay Sajip
+3  A: 

The answer that immediately comes to mind, is that this is only possible if you write an ActiveX control that runs in the client browser to obtain this information on your behalf. On the other hand it might be possible with JavaScript on the client if the javascript can instantiate a COM object that will get the information. The only other way I can think of is have a windows service that does an ARP request once the IP has been captured.

IanT8
+1  A: 

Since you're on the same subnet, you can P/Invoke GetIpNetTable to get the webserver's ARP table. If you do this real-time, no additional work would be necessary - since you're having a conversation with the client, you'll have the ARP info. Otherwise, you'd need to construct an ARP request or some IP traffic (say, a ping) to get it in the cache - and note that due to DHCP and other network vagaries (like a machine being turned off), it is possible that converting IP to MAC later will yield a different answer.

Note also that any external clients (ie., ones across a router) just won't show up in the table - so be prepared to deal with that as well. If you need a MAC for them for some reason, it's technically your router's MAC.

Mark Brackett