1

I need to get local system ip address and browser agent (firefox,chorme,ie,etc..) in MVC 5. Search google Request.ServerVariables["REMOTE_ADDR"] which is not working in MVC5

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
  • `Request.Browser` is what you need to check for browser information. `Request.UserHostAddress` is for IP Address. In both the cases, you may not be able to guarantee the right information, because proxies will not give you right IP Address of the client Machine and browsers User agent can always be manipulated. Let me know if my point helps you. – ramiramilu Jun 22 '15 at 06:02

3 Answers3

3

For getting IP Address use this code:

public static string GetIPAddress(HttpRequestBase request)
    {
        string ip;
        try
        {
            ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (!string.IsNullOrEmpty(ip))
            {
                if (ip.IndexOf(",") > 0)
                {
                    string[] ipRange = ip.Split(',');
                    int le = ipRange.Length - 1;
                    ip = ipRange[le];
                }
            }
            else
            {
                ip = request.UserHostAddress;
            }
        }
        catch { ip = null; }

        return ip;
    }

https://stackoverflow.com/a/7348761/4568359

================================================================

And for getting browser info:

System.Web.HttpBrowserCapabilities browser = Request.Browser;
string brw_info = "Browser Capabilities\n"
    + "Type = "                    + browser.Type + "\n"
    + "Name = "                    + browser.Browser + "\n"
    + "Version = "                 + browser.Version + "\n"
    + "Major Version = "           + browser.MajorVersion + "\n"
    + "Minor Version = "           + browser.MinorVersion + "\n"
    + "Platform = "                + browser.Platform + "\n"
    + "Is Beta = "                 + browser.Beta + "\n"
    + "Is Crawler = "              + browser.Crawler + "\n"
    + "Is AOL = "                  + browser.AOL + "\n"
    + "Is Win16 = "                + browser.Win16 + "\n"
    + "Is Win32 = "                + browser.Win32 + "\n"
    + "Supports Frames = "         + browser.Frames + "\n"
    + "Supports Tables = "         + browser.Tables + "\n"
    + "Supports Cookies = "        + browser.Cookies + "\n"
    + "Supports VBScript = "       + browser.VBScript + "\n"
    + "Supports JavaScript = "     + 
        browser.EcmaScriptVersion.ToString() + "\n"
    + "Supports Java Applets = "   + browser.JavaApplets + "\n"
    + "Supports ActiveX Controls = " + browser.ActiveXControls 
          + "\n"
    + "Supports JavaScript Version = " +
        browser["JavaScriptVersion"] + "\n";

https://msdn.microsoft.com/en-us/library/3yekbd5b.aspx

Community
  • 1
  • 1
2

To get client IP Address

var IPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(IPAddress))
{
    IPAddress = Request.ServerVariables["REMOTE_ADDR"];
}

To get client user agent.

var userAgent = Request.UserAgent;
Developer SPM
  • 563
  • 1
  • 4
  • 13
1

You are looking for something like to get Ip address

HttpRequest.UserHostAddress Property

and check out this for browser detection 51Degrees.Mobi Foundation

Madhawas
  • 371
  • 1
  • 2
  • 15