locked
how to use DoGetHostEntry(string hostname) in controller , view for my website ? RRS feed

  • Question

  • User1928394251 posted

    hello

    i use dns server in my server , i want to search my website with host name :http:\\webserever

    when i use under code in my website for get ipaddress client:  mozilla , ie dont show me but chrome shows .

     public ActionResult GetIpWeb()
            {
                string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (ipAddress==""||ipAddress==null)
                {
                    ipAddress = Request.ServerVariables["REMOTE_ADDR"];
                }
                ViewBag.IPAddress = ipAddress;

                return View();
            }

    i want to use under code instand of above code .

     public static void DoGetHostEntry(string hostname)
            {
                IPHostEntry host = Dns.GetHostEntry(hostname);

               // Console.WriteLine($"GetHostEntry({hostname}) returns:");
              

                foreach (IPAddress address in host.AddressList)
                {
                    // Console.WriteLine($"    {address}");
                   // return (address);
                }
            }

    but i dont khow to how write  code in controller & view in mvc .

    Monday, February 1, 2021 6:50 AM

Answers

  • User475983607 posted

    i want to get ip client that open my website in your browser . my site show ip address client  that chrome  is right but in ie , mozilla is wrong.

    Mozilla is functioning as expected. It's your understanding that's wrong. 

    how to use under code in my website ;maybe it be right .

    public static void DoGetHostEntry(string hostname)
    {
        IPHostEntry host = Dns.GetHostEntry(hostname);
    
        Console.WriteLine($"GetHostEntry({hostname}) returns:");
    
        foreach (IPAddress address in host.AddressList)
        {
            Console.WriteLine($"    {address}");
        }
    }

    The code shown runs on a web server not the client browser.  Your requirement is not possible for a browser based solution.  You have to write a custom client and not use the browser for your requirement.  As explained many times in your similar thread, the client IP address is of no use in a web application.  Why do you need the IP address?  What do you do with the IP address?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 7, 2021 11:59 AM
  • User-474980206 posted

    as we keep telling you, that is the ipaddress. it is just ipv6 format. you must convert to ipv4 if that is what you want.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 14, 2021 12:48 AM

All replies

  • User503812343 posted

    the following code will give you all IP address and record entry from your website

    var ips = System.Net.Dns.GetHostEntry("microsoft.com").AddressList;
    foreach (var ip in ips)
        Console.WriteLine(ip);
    

    Monday, February 1, 2021 7:24 AM
  • User1928394251 posted

    hello . i write website for get ip adress client in local network (for dns server  ) .

    i dont khnow to how get ip address client when write my site in browser with host name server (httpL\\webserver)

    i dont know to how use under code in controller , view :

     public static void DoGetHostEntry(string hostname)
            {
                IPHostEntry host = Dns.GetHostEntry(hostname);

                Console.WriteLine($"GetHostEntry({hostname}) returns:");(i dont know how to write in controller )????????????

                foreach (IPAddress address in host.AddressList)
                {
                    Console.WriteLine($"    {address}");?????????????????
                }
            }

    help me please ....

    Thursday, February 4, 2021 8:06 AM
  • User475983607 posted

    This is a duplicate thread.  The client IP address is generally useless in a web application because many users can have the same IP address.  You can never get the actual user's machine IP address you only get the user's gateway to the Internet or network endpoint.

    Can you explain why you need the IP address?  What is the problem you are trying to solve?

    Thursday, February 4, 2021 2:04 PM
  • User1928394251 posted

    hello . i have local network  with work group .

    i want to get ip client that open my website in your browser . my site show ip address client  that chrome  is right but in ie , mozilla is wrong.

    explain(i run my site in iis in windows server 2019, with dns server .when search my website with ipaddress server dont have problem but when search with host name server have problam in mozilla , ie  )(for example   http:\\192.168.50.86 , http:\\webserver )

    i write in controller:

      public ActionResult GetIpWeb()
            {
                string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

                if (ipAddress==""||ipAddress==null)

                {
                   // ipAddress = Request.UserHostAddress + " test "  ;

                    ipAddress = Request.ServerVariables["REMOTE_ADDR"];
                }
                ViewBag.IPAddress = ipAddress;

                return View();
            }

    in my view :

        @{Html.RenderAction("GetIpWeb", "Home");}
      

    but i want to change my controller that i can get ip from dns or show in every browsers but i dont khnow  how ????

    how to use under code in my website ;maybe it be right .

    public static void DoGetHostEntry(string hostname)
    {
        IPHostEntry host = Dns.GetHostEntry(hostname);
    
        Console.WriteLine($"GetHostEntry({hostname}) returns:");
    
        foreach (IPAddress address in host.AddressList)
        {
            Console.WriteLine($"    {address}");
        }
    }

    Sunday, February 7, 2021 6:15 AM
  • User475983607 posted

    i want to get ip client that open my website in your browser . my site show ip address client  that chrome  is right but in ie , mozilla is wrong.

    Mozilla is functioning as expected. It's your understanding that's wrong. 

    how to use under code in my website ;maybe it be right .

    public static void DoGetHostEntry(string hostname)
    {
        IPHostEntry host = Dns.GetHostEntry(hostname);
    
        Console.WriteLine($"GetHostEntry({hostname}) returns:");
    
        foreach (IPAddress address in host.AddressList)
        {
            Console.WriteLine($"    {address}");
        }
    }

    The code shown runs on a web server not the client browser.  Your requirement is not possible for a browser based solution.  You have to write a custom client and not use the browser for your requirement.  As explained many times in your similar thread, the client IP address is of no use in a web application.  Why do you need the IP address?  What do you do with the IP address?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 7, 2021 11:59 AM
  • User-474980206 posted

    to convert the ipv6 address to ipv4, check the family type

    public static string  ConvertToIpv4(string ipv6)
    {
        IPHostEntry host = Dns.GetHostEntry(ipv6);
        foreach (IPAddress address in host.AddressList)
        {
            if (address.AddressFamily.ToString() == "InterNetwork")
                 return address.ToString();
        }
        return null;
    }

    Monday, February 8, 2021 5:11 PM
  • User1928394251 posted

    hello. thanks . i get ip server when i use this code . but i want ip address client than visited my web .

    i access to ipaddress client in chrome to this code :

     public ActionResult GetIpWeb()
            {
                
               
               string ipAddress = Request.ServerVariables["REMOTE_ADDR"].ToString();
                if (string.IsNullOrEmpty(ipAddress))
                {
                    ipAddress = Request.ServerVariables["REMOTE_ADDR"];
                }
                ViewBag.IPAddress = ipAddress;

                return View();
            }
            but i cant to access to ip address client in mozilla , ie . i dont why ?????????????????????????

    i see this in my website :

    fe80::c847:bd70:ccf7:6c87%15

    Saturday, February 13, 2021 5:01 AM
  • User-474980206 posted

    as we keep telling you, that is the ipaddress. it is just ipv6 format. you must convert to ipv4 if that is what you want.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 14, 2021 12:48 AM
  • User1928394251 posted

    hello

    thank you

    my problam is right .

    ipv6 disable in windows server , it solved my problem.

    Saturday, February 20, 2021 11:26 AM