سؤال ASP.NET بشأن الحصول على عنوان IP للمستخدم
-
02-10-2019 - |
سؤال
أحاول جلب عنوان IP باستخدام هذا:-
protected void Page_Load(object sender, EventArgs e)
{
string ClientIP;
ClientIP = HttpContext.Current.Request.UserHostAddress;
Label1.Text = ClientIP;
}
يخرج هذا الرمز 127.0.0.1.
والرمز أدناه لا يعرض شيئًا!
string ClientIP;
ClientIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
Label1.Text = ClientIP;
كيف أحضر عنوان IP؟ عنوان IP الحقيقي للمستخدم؟
تعديل
لا أريد موقعًا دقيقًا بالمناسبة. أحتاج إلى معرفة البلد ثم إعادة توجيه المستخدم إلى صفحة ويب وفقًا لذلك.
المحلول
Request.Params["REMOTE_ADDR"]
نصائح أخرى
باستخدام System.net ، جرب هذا -
// Then using host name, get the IP address list..
IPHostEntry ipEntry = DNS.GetHostByName (strHostName);
IPAddress [] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
Console.WriteLine ("IP Address {0}: {1} ", i, addr[i].ToString ());
}
لا تنتمي إلى StackOverflow