Public IP address via C#

Noah Aas 460 Reputation points
2024-10-27T15:55:56.7733333+00:00

Hello,

Is there a way to read out the public IP address via C#?

[https://whatismyipaddress.com/]

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,008 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 46,456 Reputation points Microsoft Vendor
    2024-10-30T09:36:38.98+00:00

    Hi @Noah Aas , Welcome to Microsoft Q&A,

    You can use C# to get the public IP address by calling a public API. For example, you can use HttpClient to request a service that provides a public IP address (for example, https://api.ipify.org?format=json)

      public static async Task<string> GetPublicIpAddress()
        {
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    // Request API to get public IP
                    HttpResponseMessage response = await client.GetAsync("https://api.ipify.org?format=json");
                    response.EnsureSuccessStatusCode();
                    
                    // Parse the returned JSON response
                    string jsonResponse = await response.Content.ReadAsStringAsync();
                    JObject jsonObject = JObject.Parse(jsonResponse);
                    
                    // Extract IP address
                    return jsonObject["ip"].ToString();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                    return null;
                }
            }
        }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,621 Reputation points
    2024-10-27T16:54:53.2333333+00:00

    It depends on the hosting webserver. With asp.net it’s Request.UserHostAddress, but if a load balancer is used it’s in a forwarded header HTTP_X_FORWARDED_FOR.

    note: if you are trying to get the ipaddress from the computer itself, you must call out to a internet hosted webserver that returns the address.

    0 comments No comments

  2. Castorix31 85,806 Reputation points
    2024-10-27T17:36:39.3566667+00:00

    A way :

    using (HttpClient client = new HttpClient())
    {
        string sIPAddress = await client.GetStringAsync("https://api.ipify.org");
        System.Diagnostics.Debug.WriteLine(sIPAddress);
    }
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.