如何在客户端(使用javascript)和服务器端验证mac地址和ipaddress(使用c#)

我的团队使用Asp.net MVC(c#)完成与网络相关的项目.

我需要验证mac地址&客户端的ipaddress(使用javascript)&服务器端(使用c#)进行简单的表单输入.我没有得到一个很好的解决方案来验证mac地址& IP地址.

我搜索谷歌找到一个良好的用户界面验证mac地址,通过两个数字给出冒号,例如:“XX:XX:XX:XX:XX:XX”使用蒙面输入.请给出参考/指导实现这一点.用于实现Masked Input的任何jquery插件.

最佳答案
如果你需要获取地址(ip,mac)服务器端,下面的代码将帮助你:

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);
        }
}

转载注明原文:如何在客户端(使用javascript)和服务器端验证mac地址和ipaddress(使用c#) - 代码日志