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;

        }

4 comments:

  1. GetMacAddress function is not working

    ReplyDelete
    Replies
    1. I think MAC ADDRESS Not Returning properly let me check it , but public ip code is tested and working file

      Delete
  2. https://www.codeproject.com/Questions/709517/How-to-get-clients-MAC-Address-in-Asp-Net

    ReplyDelete
  3. An impressive share, I just given this onto a colleague who was doing a little evaluation on this. And he in reality bought me breakfast as a result of I found it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to discuss this, I really feel strongly about it and love studying extra on this topic. If potential, as you develop into expertise, would you mind updating your weblog with extra particulars? It’s extremely useful for me. Huge thumb up for this weblog submit! ux san francisco

    ReplyDelete

Thank You For Your Great Contribution

Featured Post

How to remove multiple item from FormArray

 How to remove multiple item from FormArray Call Method at any component :- this.removeItems(formArray, formcontrolCountryID, 2);  Meth...

Popular Posts