Get Client IP Address In ASP.NET MVC

We can use HttpRequest.ServerVariables to get the IP address of a client in ASP.NET MVC. The REMOTE_ADDR variable gives the IP address of the client. 

You can directly use the below method to your controller page and call it from your view or whereever you need it. 

Public string GetIp()  
{  
    string ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
    if (string.IsNullOrEmpty(ip))  
    {  
        ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
    }  
    return ip;  
}

There are other methods to get an IP Address. Read more details here:  Get IP Address In ASP.NET.

The above method returns the client IP address. It is better to use this method than the Request.UserHostAddress() because UserHostAddress sometimes may capture the IP address of user's proxy.

HttpRequest.ServerVariables not only gets the IP address but returns many other meta data about the client browser and other details including details about client browser, server, user, and URLs. Some of the common meta data includes server name, server software, server port, logged in user, cookies info, header info, HTTPS info, authenticated user and authentication method, and others.

Here is a list of variables and their meaning returned by the HttpRequest.ServerVariables. 

  1. ALL_HTTP: All HTTP headers sent by the client.
  2. ALL_RAW: Retrieves all headers in raw form. The difference between ALL_RAW and ALL_HTTP is that ALL_HTTP places an HTTP_ prefix before the header name and the header name is always capitalized. In ALL_RAW the header name and values appear as they are sent by the client.
  3. APP_POOL_ID: Returns the name of the application pool that is running in the IIS worker process that is handling the request. There is also an APP_POOL_ID environment variable that is available to applications that are running in the IIS worker process.
  4. APPL_MD_PATH: Retrieves the metabase path of the application.
  5. APPL_PHYSICAL_PATH: Retrieves the physical path corresponding to the metabase path in APPL_MD_PATH.
  6. AUTH_PASSWORD: The value entered in the client's authentication dialog. This variable is available only if Basic authentication is used.
  7. AUTH_TYPE: The authentication method that the server uses to validate users when they attempt to access a protected script. It does not mean that the user was authenticated if AUTH_TYPE contains a value and the authentication scheme is not Basic or integrated Windows authentication. The server allows authentication schemes it does not natively support because an ISAPI filter may be able to handle that particular scheme.
  8. AUTH_USER: The name of the user as it is derived from the authorization header sent by the client, before the user name is mapped to a Windows account. This variable is no different from REMOTE_USER. If you have an authentication filter installed on your Web server that maps incoming users to accounts, use LOGON_USER to view the mapped user name.
  9. CACHE_URL: For use in ISAPI applications only. Returns the unambiguous name for the current URL. It is necessary to use the Unicode version of this variable in conjunction with the kernel mode cache invalidation function to evict entries placed in the cache by HSE_REQ_VECTOR_SEND.
  10. CERT_COOKIE: Unique ID for the client certificate, returned as a string. This can be used as a signature for the whole client certificate.
  11. CERT_FLAGS: bit0 is set to 1 if the client certificate is present. bit1 is set to 1 if the certification authority of the client certificate is invalid (that is, it is not in the list of recognized certification authorities on the server). If bit 1 of CERT_FLAGS is set to 1, indicating that the certificate is invalid, IIS version 4.0 and later will reject the certificate. Earlier versions of IIS will not reject the certificate.
  12. CERT_ISSUER: Issuer field of the client certificate (O=MS, OU=IAS, CN=user name, C=USA).
  13. CERT_KEYSIZE: Number of bits in the Secure Sockets Layer (SSL) connection key size. For example, 128.
  14. CERT_SECRETKEYSIZE: Number of bits in server certificate private key. For example, 1024.
  15. CERT_SERIALNUMBER: Serial number field of the client certificate.
  16. CERT_SERVER_ISSUER: Issuer field of the server certificate.
  17. CERT_SERVER_SUBJECT: Subject field of the server certificate.
  18. CERT_SUBJECT: Subject field of the client certificate.
  19. CONTENT_LENGTH: The length of the content as given by the client.
  20. CONTENT_TYPE: The data type of the content. Used with queries that have attached information, such as the HTTP queries GET, POST, and PUT.
  21. GATEWAY_INTERFACE: The revision of the CGI specification used by the server. The format is CGI/revision.
  22. HEADER_<HeaderName>: The value stored in the header <HeaderName>. Any header other than those listed in this table must be preceded by "HEADER_" in order for the ServerVariables collection to retrieve its value. This is useful for retrieving custom headers.
  23. HTTP_<HeaderName>: The value stored in the header <HeaderName>. Any header other than those listed in this table must be preceded by "HTTP_" in order for the ServerVariables collection to retrieve its value. This is useful for retrieving custom headers.
  24. HTTP_ACCEPT: Returns the value of the Accept header that contains a list of accepted formats, for example, "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel". The values of the fields for the HTTP_ACCEPT variable are concatenated, and separated by a comma (,).
  25. HTTP_ACCEPT_ENCODING: Returns a list of accepted encoding types, for example, "gzip, deflate".
  26. HTTP_ACCEPT_LANGUAGE: Returns a string describing the language to use for displaying content.
  27. HTTP_CONNECTION: Returns a string describing the connection type, for example, "Keep-Alive".
  28. HTTP_COOKIE: Returns the cookie string that was included with the request.
  29. HTTP_HOST: Returns the name of the Web server. This may or may not be the same as SERVER_NAME depending on type of name resolution you are using on your Web server (IP address, host header).
  30. HTTP_METHOD: The method used to make the request (same as REQUEST_METHOD).
  31. HTTP_REFERER: Returns a string that contains the URL of the page that referred the request to the current page using an HTML <A> tag. Note that the URL is the one that the user typed into the browser address bar, which may not include the name of a default document. If the page is redirected, HTTP_REFERER is empty. HTTP_REFERER is not a mandatory member of the HTTP specification.
  32. HTTP_URL: Returns the raw, encoded URL, for example, "/vdir/default.asp?querystring".
  33. HTTP_USER_AGENT: Returns a string describing the browser that sent the request.
  34. HTTP_VERSION: The name and version of the request protocol (the raw form of SERVER_PROTOCOL).
  35. HTTPS: Returns ON if the request came in through a secure channel (for example, SSL); or it returns OFF, if the request is for an insecure channel.
  36. HTTPS_KEYSIZE: Number of bits in the SSL connection key size. For example, 128.
  37. HTTPS_SECRETKEYSIZE: Number of bits in the server certificate private key. For example, 1024.
  38. HTTPS_SERVER_ISSUER: Issuer field of the server certificate.
  39. HTTPS_SERVER_SUBJECT: Subject field of the server certificate.
  40. INSTANCE_ID: The ID for the IIS instance in textual format. If the instance ID is 1, it appears as a string. You can use this variable to retrieve the ID of the Web server instance (in the metabase) to which the request belongs.
  41. INSTANCE_META_PATH: The metabase path for the instance of IIS that responds to the request.
  42. LOCAL_ADDR: Returns the server address on which the request came in. This is important on computers where there can be multiple IP addresses bound to the computer, and you want to find out which address the request used.
  43. LOGON_USER: The Windows account that the user is impersonating while connected to your Web server. Use REMOTE_USER, UNMAPPED_REMOTE_USER, or AUTH_USER to view the raw user name that is contained in the request header. The only time LOGON_USER holds a different value than these other variables is if you have an authentication filter installed.
  44. PATH_INFO: Path information, as given by the client, for example, "/vdir/myisapi.dll/zip". If this information comes from a URL, it is decoded by the server before it is passed to the CGI script or ISAPI filter.
  45. If the AllowPathInfoForScriptMappings metabase property is set to true (to support exclusive CGI functionality), PATH_INFO will only contain "/zip" and ISAPI applications such as ASP will break.
  46. PATH_TRANSLATED: The physical path that maps to the virtual path in PATH_INFO, for example, "c:\inetpub\wwwroot\vdir\myisapi.dll". This variable is used by IIS during the processing of ISAPI applications.
  47. If the AllowPathInfoForScriptMappings metabase property is set to true (to support exclusive CGI functionality), PATH_INFO will only contain "/zip" and ISAPI applications such as ASP will break.
  48. QUERY_STRING: Query information stored in the string following the question mark (?) in the HTTP request.
  49. REMOTE_ADDR: The IP address of the remote host that is making the request.
  50. REMOTE_HOST: The name of the host that is making the request. If the server does not have this information, it will set REMOTE_ADDR and leave this empty.
  51. REMOTE_PORT: The client port number of the TCP connection.
  52. REMOTE_USER: The name of the user as it is derived from the authorization header sent by the client, before the user name is mapped to a Windows account. If you have an authentication filter installed on your Web server that maps incoming users to accounts, use LOGON_USER to view the mapped user name.
  53. REQUEST_METHOD: The method used to make the request. For HTTP, this can be GET, HEAD, POST, and so on.
  54. SCRIPT_NAME: A virtual path to the script being executed, for example, "/vdir/default.asp". This is used for self-referencing URLs.
  55. SCRIPT_TRANSLATED: The canonical physical path to the script listed in SCRIPT_NAME, for example, "\\?\c:\inetpub\wwwroot\vdir\default.asp".
  56. SERVER_NAME: The server's host name, DNS alias, or IP address as it would appear in self-referencing URLs.
  57. SERVER_PORT: The server port number to which the request was sent.
  58. SERVER_PORT_SECURE: A string that contains either 0 or 1. If the request is being handled on the secure port, then this is 1. Otherwise, it is 0.
  59. SERVER_PROTOCOL: The name and revision of the request information protocol. The format is protocol/revision. (The canonicalized form of HTTP_VERSION.)
  60. SERVER_SOFTWARE: The name and version of the server software that answers the request and runs the gateway. The format is name/version.
  61. SSI_EXEC_DISABLED: IIS 5.1 and earlier: This server variable is not available.
  62. Returns a 1 if the server-side include directive, #exec, is disabled. Otherwise, SSI_EXE_DISABLED returns a 0. To enable or disable #exec, use the SSIExecDisablemetabase property.
  63. UNENCODED_URL: Returns the raw, unencoded URL, for example, "/vdir/default.asp?querystring".
  64. UNICODE_<ServerVariable Name>. In unicode ISAPI applications only, it is possible to retrieve server variable values as unicode values by prepending "UNICODE_" to the name of the server variable, unless the variable starts with "HTTP_" or "HEADER_".
  65. UNMAPPED_REMOTE_USER: The name of the user as it is derived from the authorization header sent by the client, before the user name is mapped to a Windows account (same as REMOTE_USER). If you have an authentication filter installed on your Web server that maps incoming users to accounts, use LOGON_USER to view the mapped user name. 
  66. URL: Gives the base portion of the URL, without any querystring or extra path information, for example, "/vdir/default.asp". For the raw URL, use HTTP_URL or UNENCODED_URL.