Skip to main content

ASP.NET C# page that retrieves the public IP of the host server.

<%@ Page ContentType="text/plain" Language="C#" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<script runat=server>
    public string GetExternalIp()
    {
        try
        {
            string externalIP;

            externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org");
            externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")).Matches(externalIP)[0].ToString();

            return externalIP;
        }
        catch
        {
            return "";
        }
    }
</script>
<%=GetExternalIp()%>