AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Monday 7 December 2015

Get client IP address & access types in asp.net

Client side IP address & access types is required to maintains log information in website or apps. You can get Client IP address & access type by following code easily.
Client IP Address

public static string GetIPAddress()
      {
          try
          {
              System.Web.HttpContext context = System.Web.HttpContext.Current;
              string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
 
              if (!string.IsNullOrEmpty(ipAddress))
              {
                  string[] addresses = ipAddress.Split(',');
                  if (addresses.Length != 0)
                  {
                      return addresses[0];
                  }
              }
              return context.Request.ServerVariables["REMOTE_ADDR"];
          }
          catch
          {
             return "";
          }
      }

GetAccessType

public static string GetAccessType()
      {
          System.Web.HttpBrowserCapabilities browser = System.Web.HttpContext.Current.Request.Browser;
          string s = "";
          try
          {
              s = "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";
          }
          catch
          { }
          return s;
      }
I hope this article is helpful for you.

0 comments :

Post a Comment

  • Popular Posts
  • Comments