How to check MAC address and ipaddress on client side (using javascript) and server side (using C #)

My team is doing a network related project using Asp.net MVC (C #).

I need to check the mac address and ipaddress on the client side (using javascript) and on the server side (using C #) for a simple form entry. I did not get a good solution for checking addresses and IP addresses.

I searched google to find a good user interface for checking the MAC address, giving a colon after two digits, for example: "XX: XX: XX: XX: XX: XX" using Masked Input. Please give a link / guide for implementing this, Any jquery plugin for implementing Masked Input.

+7
javascript c # asp.net-mvc
source share
2 answers

If you need to get the server (ip, mac) address, the code below will help you:

public partial class RemoteClientInfo : System.Web.UI.Page { public class NetUtils { //http://msdn.microsoft.com/en-us/library/aa366358(VS.85).aspx [System.Runtime.InteropServices.DllImport("iphlpapi.dll", ExactSpelling = true)] public static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref uint PhyAddrLen); private static System.Net.IPAddress GetIpAddress(string address) { System.Net.IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(address); if (hostEntry != null) { foreach (System.Net.IPAddress ipAddress in hostEntry.AddressList) { if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { return ipAddress; } } } return null; } public static string GetMacAddress(string address) { System.Net.IPAddress ipAddress = GetIpAddress(address); if (ipAddress != null) { byte[] addressBytes = ipAddress.GetAddressBytes(); byte[] macAddress = new byte[6]; uint macAddressLen = (uint)macAddress.Length; if (SendARP(BitConverter.ToInt32(addressBytes, 0), 0, macAddress, ref macAddressLen) == 0) { return BitConverter.ToString(macAddress); } } return null; } } protected void GetClientInfoButton_Click(object sender, EventArgs e) { string remoteIp = System.Web.HttpContext.Current.Request.UserHostAddress; string remoteMacAddr = NetUtils.GetMacAddress(remoteIp); this.InfoTextBox.Text = string.Format("ip=[{0}] mac=[{1}]", remoteIp, remoteMacAddr); } } 
+2
source share

Are you trying to check the format or actual addresses?

if first, try regualar expressions ...

IP address: \ b (25 [0-5] | 2 [0-4] [0-9] |? [01] [0-9] [0-9]?) (25 [0-5] | . 2 [0 -4] [0-9] | [01] [0-9] [0-9]) (25 [0-5] | ?. 2 [0-4] [0-9] | [ 01]? [0-9] [0-9]) (25 [0-5] | ?. 2 [0-4] [0-9] | [01] [0-9] [0-9]) \? b

Or look here for MAC addresses

0
source share

All Articles