How To Get IP Address And MAC Address Using c# , Aps.net MVC


How To Get IP Address And MAC Address Using c# , Aps.net MVC


public ActionResult Index()
{
    var IP = GetUser_IP();

            ViewBag.Ip = IP;

            ViewBag.Mac=GetMacAddress(IP);

            return View();

}



[NonAction]

        protected string GetUser_IP()

        {

   string VisitorsIPAddr = string.Empty;

    if (HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)

            {

        VisitorsIPAddr =HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();

            }

            else if (HttpContext.Request.UserHostAddress.Length != 0)

            {

                VisitorsIPAddr = HttpContext.Request.UserHostAddress;

            }

           return  VisitorsIPAddr;

        }



     

   [NonAction]

        public string GetMacAddress(string ipAddress)

        {

            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

            String sMacAddress = string.Empty;

            foreach (NetworkInterface adapter in nics)

            {

                if (sMacAddress == String.Empty)// only return MAC Address from first card  

                {

                    IPInterfaceProperties properties = adapter.GetIPProperties();

                    sMacAddress = adapter.GetPhysicalAddress().ToString();

                }

            }

            return sMacAddress;

        }

No comments:

Post a Comment

Thank You For Your Great Contribution

Featured Post

Sql dynamic query With Join And search using like query with multiple column

Sql dynamic query With Join And search using like query with multiple column declare @keyword varchar(100) DECLARE @sqlCommand va...

Popular Posts