c# - 如何获取IP地址?

标签 c# .net asp.net

我想获取在我的网站上注册的人的 ip 地址。如何在 ASPNET 中执行此操作。我使用了以下代码,但是它没有获得正确的 IP 地址

string ipaddress = Request.UserHostAddress;

最佳答案

您可以使用此方法获取客户端机器的IP地址。

public static String GetIP()
{
    String ip = 
        HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (string.IsNullOrEmpty(ip))
    {
        ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }

    return ip;
}

关于c# - 如何获取IP地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1907195/

相关文章:

c# - 将 blob (.bacpac) 转换为 .bacpac 文件以将数据库导入到 SQL Server Azure?

c# - 将两种方法重构为一种

c# - 如果我想使用 ASP.NET MVC4 创建 ApiKey 受限资源,我应该使用 IAuthorizationFilter 吗?

c# - Windows 服务无法停止

asp.net - httpRuntime targetFramework ="4.5"导致 500 - 内部服务器错误

c# - ArgumentOutOfRangeException 使用 IndexOf 和 CultureInfo 1031

c# - C#如何获取字符串中的所有其他字符

c# - 如何正确处理 "existing connection forcibly closed"

c# - 将查询字符串结果绑定(bind)到服务器标签并设置按钮的可见属性

asp.net - IsPostback=false 即使 Request.HttpMethod 是 "POST"?