In a situation where you use an IP address for security, you should be aware of your infrastructure.
If you use a proxy between your web server and your clients, which sets the header, you should be able to trust the last address. Then you use the code, for example, proposed by Muhammad with the update, to always get the latest IP address from the direct header (see Code below).
If you are not using a proxy server, be careful that the X-Forwarded-For header is very easy to trick. I suggest you ignore this if you have no clear reason why not.
I updated the code of Muhammad Akhtar as follows so that you can choose:
public string GetIP(bool CheckForward = false) { string ip = null; if (CheckForward) { ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; } if (string.IsNullOrEmpty(ip)) { ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } else {
This Wikipedia article explains the risks more thoroughly.
Wouter Simons Nov 06 2018-12-12T00: 00Z
source share