如何在c#中获取登录用户的IP地址 [英] how to get ip address of logged in user in c#
本文介绍了如何在c#中获取登录用户的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获得ip登录我网站的用户的地址。任何人都可以帮助我这样做。
我正在使用代码但它正在返回:: 1
这是我的代码:
Hi,
I want to get ip Address of user who loggend into my website. Can anybody plz help me to do so.
I'm using code but it is returning ::1
Here is my code:
public static string getclientIP()
{
string result = string.Empty;
string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ip))
{
string[] ipRange = ip.Split(',');
int le = ipRange.Length - 1;
result = ipRange[0];
}
else
{
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
return result;
}
推荐答案
使用inetmgr配置您的IP
Configure your IP with "inetmgr"
rivate string GetUserIP()
{
return Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.ServerVariables["REMOTE_ADDR"];
}
谢谢&注意
Sham
Thanks & Regard
Sham
你只需试试这个代码
使用这个dll
使用System.Net;
You Just Try this code
use this dll
using System.Net;
public string GetIP()
{
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
return addr[addr.Length-1].ToString();
}
这篇关于如何在c#中获取登录用户的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文