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;

        }

6 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
  4. I know learning the 3 new versions of IPv6 addresses sounds very confusing; believe you me, I was once in your shoes.my ip address tracker

    ReplyDelete
  5. An IP address is a product address and intended to permit have on one organization to speak with a host on an alternate organization no matter what the kind of LANs the hosts are partaking in.https://onohosting.com/

    ReplyDelete

Thank You For Your Great Contribution

Featured Post

How To Dynamic Upload multiple files with extra detail of model in asp.net core

 How To Dynamic Upload multiple files with extra detail of model in asp.net core  1). firstly create Model File  namespace MyModel {     pub...

Popular Posts