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.