ASP.NET擷取IP和MAC代碼(C#)

來源:互聯網
上載者:User

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Management;
using System.Management.Instrumentation;
using System.Runtime.Remoting.Contexts;
using System.Web;

namespace ComLib
{
    public class GetIP_MAC
    {
        [DllImport("Iphlpapi.dll")]
        private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
        [DllImport("Ws2_32.dll")]
        private static extern Int32 inet_addr(string ip);

 

        public static string GetIP()
        {
            string ip = "";
            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null) // 伺服器, using proxy
            {
                //得到真實的用戶端地址
                ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP.
            }
            else//如果沒有使用Proxy 伺服器或者得不到用戶端的ip not using proxy or can't get the Client IP
            {

                //得到服務端的地址
                ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
            }
            return ip;
        }
        /// <summary>
        /// 擷取MAC地址
        /// </summary>
        /// <returns></returns>
       public static string GetMAC()
        {
            try
            {
                string userip = System.Web.HttpContext.Current.Request.UserHostAddress;
                string strClientIP = System.Web.HttpContext.Current.Request.UserHostAddress.ToString().Trim();
                Int32 ldest = inet_addr(strClientIP); //目的地的ip
                Int32 lhost = inet_addr("");   //本機伺服器的ip
                Int64 macinfo = new Int64();
                Int32 len = 6;
                int res = SendARP(ldest, 0, ref macinfo, ref len);
                string mac_src = macinfo.ToString("X");
                string mac_dest = "";
                //if (mac_src == "0")
                //{
                //    if (userip == "127.0.0.1")
                //        mac_dest = "正在訪問Localhost!";
                //    else
                //        mac_dest = "歡迎來自IP為" + userip + "的朋友!" + "<br>";
                //    return mac_dest;
                //}

                while (mac_src.Length < 12)
                {
                    mac_src = mac_src.Insert(0, "0");
                }              

                for (int i = 0; i < 11; i++)
                {
                    if (0 == (i % 2))
                    {
                        if (i == 10)
                        {
                            mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
                        }
                        else
                        {
                            mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
                        }
                    }
                }

                //Response.Write("歡迎來自IP為" + userip + "<br>" + ",MAC地址為" + mac_dest + "的朋友!" + "<br>");
                return mac_dest;

            }
            catch
            {
                return "找不到主機資訊";
            }
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.