Menu ▼



Posted By :
avatarmrkraju
Total Views : 2495

Get client or end user ip address, operating system, browser name and version in asp.net and c#

This example will show you how to get client/end user ip address, operating system, browser name and browser version in asp.net and c#.

CS Code
Below c# code will get the client ip address, operating system, browser name and version details in asp.net page load event using c#.

protected void Page_Load(object sender, EventArgs e)
{
    HttpBrowserCapabilities browserCapabilities = Request.Browser;

    //getting client ip address
    string ipAddress = HttpContext.Current.Request.UserHostAddress;

    //getting client browser name
    string browserName = browserCapabilities.Browser;

    //getting client browser version
    string browserVersion = browserCapabilities.Version;

    //getting client operating system
    string operatingSystem = getOperatinSystemDetails(Request.UserAgent);
}

public static string getOperatinSystemDetails(string browserDetails)
{
    try
    {
        switch (browserDetails.Substring(browserDetails.LastIndexOf("Windows NT") + 11, 3).Trim())
        {
            case "6.2":
                return "Windows 8";
            case "6.1":
                return "Windows 7";
            case "6.0":
                return "Windows Vista";
            case "5.2":
                return "Windows XP 64-Bit Edition";
            case "5.1":
                return "Windows XP";
            case "5.0":
                return "Windows 2000";
            default:
                return browserDetails.Substring(browserDetails.LastIndexOf("Windows NT"), 14);
        }
    }
    catch
    {
        if (browserDetails.Length > 149)
            return browserDetails.Substring(0, 149);
        else
            return browserDetails;
    }
}






comments powered by Disqus


Search dotnetlearners.com