登录系统的IPAddress
使用波纹管代码。
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
的IPaddress。
如何获取系统的正确IP地址。
如果您在Web服务器上使用它是localhost
::1
,您将获得正确的。
虽然它取决于用户访问您的应用程序的网络配置。
可能存在不暴露客户端系统的实际IP的firewall
。
获取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(); } } }
要获取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; }
资源
客户端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
您可以通过以下方式找到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”。
希望它对你有所帮助。
:: 1是IPv6本地主机地址,您可以在此处找到更多信息