الحصول على عنوان IP من ملقم في ASP.NET؟
سؤال
وكيف يمكنني الحصول على عنوان IP من الملقم الذي يدعو صفحة ASP.NET الخاص بي؟ لقد رأيت الاشياء عن كائن الاستجابة، ولكن أنا جديد جدا في ج #. شكرا للطن.
المحلول
وهذا يجب أن تعمل:
//this gets the ip address of the server pc
public string GetIPAddress()
{
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); // `Dns.Resolve()` method is deprecated.
IPAddress ipAddress = ipHostInfo.AddressList[0];
return ipAddress.ToString();
}
HTTP: //wec-library.blogspot.com/2008/03/gets-ip-address-of-server-pc-using-c.html
وOR
//while this gets the ip address of the visitor making the call
HttpContext.Current.Request.UserHostAddress;
HTTP: //www.geekpedia. كوم / KB32_How دو-I-الحصول على رأس زوار-IP-address.html
نصائح أخرى
وRequest.ServerVariables["LOCAL_ADDR"];
وهذا يعطي IP جاء الطلب في يوم للخوادم متعددة المخزن
ما سبق هو بطيء لأنه يتطلب مكالمة DNS (وبالطبع لن تعمل إذا لم يتوفر). يمكنك استخدام رمز أدناه للحصول على خريطة عناوين IPV4 المحلية الكمبيوتر الحالي مع قناع الشبكة الفرعية يناظرها:
public static Dictionary<IPAddress, IPAddress> GetAllNetworkInterfaceIpv4Addresses()
{
var map = new Dictionary<IPAddress, IPAddress>();
foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (var uipi in ni.GetIPProperties().UnicastAddresses)
{
if (uipi.Address.AddressFamily != AddressFamily.InterNetwork) continue;
if (uipi.IPv4Mask == null) continue; //ignore 127.0.0.1
map[uipi.Address] = uipi.IPv4Mask;
}
}
return map;
}
تحذير: هذا غير مطبق في مونو بعد م>
//this gets the ip address of the server pc
public string GetIPAddress()
{
string strHostName = System.Net.Dns.GetHostName();
//IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); <-- Obsolete
IPHostEntry ipHostInfo = Dns.GetHostEntry(strHostName);
IPAddress ipAddress = ipHostInfo.AddressList[0];
return ipAddress.ToString();
}
وهذا العمل ل IPv4:
public static string GetServerIP()
{
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress address in ipHostInfo.AddressList)
{
if (address.AddressFamily == AddressFamily.InterNetwork)
return address.ToString();
}
return string.Empty;
}
لا تنتمي إلى StackOverflow