登录系统的IPAddress

时间:2014-02-10 04:37:46

标签: c# asp.net ip

使用以下代码。

protected string GetUserIP()
{
    string strUserIP = string.Empty;
    if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
    {
        strUserIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
    }
    else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
    {
        strUserIP = HttpContext.Current.Request.UserHostAddress;
    }
    return strUserIP;
}

我得到的格式为::1格式。

如何获取系统的正确IP地址。

6 个答案:

答案 0 :(得分:1)

如果您在网络服务器上使用,则为localhost ::1,您将获得正确的。{/ p>

虽然它取决于用户访问您的应用程序的网络配置。

可能firewall不会暴露客户端系统的实际IP。

答案 1 :(得分:1)

获取IP地址的方法:ASP.NET,C#

using System;
using System.Web;

namespace WebApplication1
{
    public class Global : HttpApplication
    {
      protected void Application_BeginRequest(object sender, EventArgs e)
      {
        // Get request.
        HttpRequest request = base.Request;

        // Get UserHostAddress property.
        string address = request.UserHostAddress;

        // Write to response.
        base.Response.Write(address);

        // Done.
        base.CompleteRequest();
      }
    }
}

答案 2 :(得分:0)

要获取IpAddress,请使用以下代码

string IPAddress = GetIPAddress();
    public string GetIPAddress()
    {

        IPHostEntry Host = default(IPHostEntry);
        string Hostname = null;
        Hostname = System.Environment.MachineName;
        Host = Dns.GetHostEntry(Hostname);
        foreach (IPAddress IP in Host.AddressList) {
            if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) {
                IPAddress = Convert.ToString(IP);
            }
        }
        return IPAddress;

    }

<强> Source

答案 3 :(得分:0)

可以从请求中读取客户端IP:

context.Request.ServerVariables["REMOTE_HOST"] 

以下是获取客户端IP地址以外的代码:

string browserInfo =
             "RemoteUser=" + context.Request.ServerVariables["REMOTE_USER"] + ";\n"
            + "RemoteHost=" + context.Request.ServerVariables["REMOTE_HOST"] + ";\n"
            + "Type=" + context.Request.Browser.Type + ";\n"
            + "Name=" + context.Request.Browser.Browser + ";\n"
            + "Version=" + context.Request.Browser.Version + ";\n"
            + "MajorVersion=" + context.Request.Browser.MajorVersion + ";\n"
            + "MinorVersion=" + context.Request.Browser.MinorVersion + ";\n"
            + "Platform=" + context.Request.Browser.Platform + ";\n"
            + "SupportsCookies=" + context.Request.Browser.Cookies + ";\n"
            + "SupportsJavaScript=" + context.Request.Browser.EcmaScriptVersion.ToString() + ";\n"
            + "SupportsActiveXControls=" + context.Request.Browser.ActiveXControls + ";\n"
            + "SupportsJavaScriptVersion=" + context.Request.Browser["JavaScriptVersion"] + "\n";

(或)

string IPAddress = string.Empty;
string SearchName = string.Empty;

String strHostName = HttpContext.Current.Request.UserHostAddress.ToString();

IPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

ServerVariables

答案 4 :(得分:0)

您可以通过以下方式找到IP地址。这个对我有用 :) 如果您只想要一个IP,那么从列表中选择第一个IP。

var Dnshost = Dns.GetHostEntry(Dns.GetHostName());

string ipAddress = "";

IPAddress[] ipAddress = Dnshost.AddressList;
ipAddress = ipAddress[0].ToString();

这里“ipAddress [0]”将为您提供系统的当前IP。如果你想要所有IP,那么迭代AddressList,如下所示:

foreach (var ip in Dnshost.AddressList)
 {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                ipAddress = ip.ToString();
            }
 }

注意:如果是“AddressFamily.InterNetwork”,它会为您提供IPv4地址,如果您需要IPv6地址,您将使用“AddressFamily.InterNetworkV6”。

希望它对你有所帮助。

答案 5 :(得分:0)

:: 1是IPv6本地主机地址,您可以找到更多信息here