none
c# ClientIP With .Contains RRS feed

  • Question

  • I'm trying to get the user's ip address, and how to include them to a string with .Contains function?**
    P.S. I Need that the script get the user ip, and if its in the list the form will load.

         if (new string[] { "85.46.118.0", "85.416.14.0", "85.512.516.6" }.Contains(!ClientIP.StartsWith))
                {
                    Image img = Properties.Resources.Good_Pincode_48px; // Right'as
                    metroTextBox1.Icon = img;
                    //////////////////////////////

                }
                else
                {
                    // new wrong().Show();
                    Image img = Properties.Resources.Wrong_Pincode_48px; // Wrong'as
                    metroTextBox1.Icon = img;

                }
    Thursday, May 17, 2018 6:38 PM

All replies

  • Are you talking about an ASP page here?  Are you getting the IP address from context.Request.ServerVariables["REMOTE_ADDR"]?  If so, then that's the IP address, as a string.  I don't know what you were trying to do with StartsWith, but that's the wrong answer.  Just use the whole string.

    However, you're matching against 85.46.118.0, and you cannot get an IP address that ends with a 0.  That's reserved as a subnet indicator.  Are you trying to match the incoming IP address against a RANGE of IP addresses -- against a whole subnet?  That cannot be done with simple string matching.  If that's what you want to do, follow up with more details and we can advise you.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Thursday, May 17, 2018 9:33 PM
  • Not sure if I have understood, but maybe something like (wtih ClientIP as IPAddress) :
    List<string> IPList = new List<string>(new string[] {"85.46.118.0", "85.416.14.0","85.512.516.6"});
    if (IPList.Exists(x => string.Equals(x, ClientIP.ToString())))
    {
               // Code
    }

    • Edited by Castorix31 Thursday, May 17, 2018 9:56 PM
    Thursday, May 17, 2018 9:55 PM
  • now working

    Should be something like this:

        //!ClientIP.StartsWith - This is the function which gets the ip.

                string ClientIP = Gaunamip();
                List<string> IPList = new List<string>(new string[] { "85.46.118.0", "85.416.14.0", "85.512.516.6" });
                if (IPList.Exists(x => string.Equals(x, ClientIP.ToString())))
                {
                    Application.Exit();
                }

    private string Gaunamip()
            {
                string ip = new WebClient().DownloadString("http://icanhazip.com");
                ip = ip.Replace("\n", "");
                return ip;
            }

    Friday, May 18, 2018 9:54 PM
  • Yes, but it's not necessary to create the List<> at all.

    string[] IPList = new string[] {"85.46.118.0", "85.416.14.0", "85.512.516.6"};
    if( Array.Exists( IPList, x=> string.Equals(x,ClientIP) )


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Monday, May 21, 2018 8:08 PM