2014-02-10 37 views
0

使用以下代码。登录系统的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; 
} 

我得到的名称IP地址一样的格式::1

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

回答

1

这是localhost::1如果您使用的网络服务器上,你会得到正确的。

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

可能有firewall它不公开客户端系统的实际IP。

0

为了得到Ip地址使用下面的代码

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

+0

为什么downvotes? –

+0

这将返回服务器的IP地址,而不是客户端的IP地址。 –

+0

@SimonWhitehead这将返回超出代理的IP,即客户端机器IP将返回.. !!我检查了它..!并且已经在实时项目中实施了。它工作正常 –

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(); 
     } 
    } 
} 
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

0

您可以通过下面的方式找到IP地址。它适用于我:) 如果你只想要一个IP,那么从列表中选择第一个IP。

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

string ipAddress = ""; 

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

这里“ipAddress [0]”会给你系统的当前IP。如果你想获得再迭代AddressList中,如下图所示所有IP的:

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

注:它会给你的IPv4地址中的“AddressFamily.InterNetwork”的情况下,如果你需要的IPv6地址,你会使用“AddressFamily.InterNetworkV6”。

希望它对您有所帮助。

0

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