none
C# How to get ip and check it with if function RRS feed

  • Question

  • Hello,

    i need a script which get the IP Address and checks if its the IP that need for login, for exmpl. 85.14.539... And if the ip don't match the form will exit.

    The code:

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

    Sunday, May 6, 2018 8:14 PM

Answers

  • 85.14.539

    85.14.539 is not valid in IPv4

    So assumed it's 85.14.53

                string ClientIP = GetIPAddress();
                if (!ClientIP.StartsWith("85.14.53"))
                {
                    MessageBox.Show("IP doesn't Match with 85.14.53.XXX\nForm will close now!\nYour IP is: "+ClientIP);
                    this.Close();
                    //Application.Exit();        Application Exit
                }
                else
                {
                    MessageBox.Show("IP Match");
                    //Proceed with Execution
                }
    


    • Marked as answer by GeyBen420 Monday, May 7, 2018 7:18 AM
    Monday, May 7, 2018 2:36 AM

All replies

  • 85.14.539

    85.14.539 is not valid in IPv4

    So assumed it's 85.14.53

                string ClientIP = GetIPAddress();
                if (!ClientIP.StartsWith("85.14.53"))
                {
                    MessageBox.Show("IP doesn't Match with 85.14.53.XXX\nForm will close now!\nYour IP is: "+ClientIP);
                    this.Close();
                    //Application.Exit();        Application Exit
                }
                else
                {
                    MessageBox.Show("IP Match");
                    //Proceed with Execution
                }
    


    • Marked as answer by GeyBen420 Monday, May 7, 2018 7:18 AM
    Monday, May 7, 2018 2:36 AM
  • Well if it's not the IP for login and your getting it from http://icanhazip.com then what is the purpose of it really?

    La vida loca

    Monday, May 7, 2018 3:13 AM
  • In case of Web pages (ASP), check the techniques based on UserHostAddress, ServerVariables and GetHostName: https://forums.asp.net/t/892765.aspx.

    Give more details about the kind of your program.

    Monday, May 7, 2018 4:48 AM