Merhaba,
İçeriğimizin başlığında her ne kadar Asp.NET MVC’ye değinilmiş olunsada aslında Asp.NET Web Forms mimarisinde de uygulayabileceğiniz bir kod demecinden bahsedeceğim.
Aşağıda vermiş olduğum kod bloğundaki yapı sayesinde ilgili kullanıcının doğruluk payı en yüksek olan IP adresini elde edebilirsiniz. Koda göz atarsanız eğer göreceksiniz ki “Load Balancer-Yük Dengeleme” yahut yönlendirme gibi olası durumları göze alarak belirli kontroller sağlanıp en doğru IP adresi elde edilmeye çalışılmıştır.
public static string GetClientIp()
{
var ipAddress = string.Empty;
if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
ipAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
else if (HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"] != null && HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"].Length != 0)
ipAddress = HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"];
else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
ipAddress = HttpContext.Current.Request.UserHostName;
return ipAddress;
}
Sevgiler…
İyi çalışmalar…
