Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using list of ip address in my webconfig file as

XML
<appSettings >
   <add key="RestrictIPAddress" value="192.0.0.110"/>
   <add key="RestrictIPAddress1" value="192.168.78.0 to 192.168.78.50"/>
   <add key="RestrictIPAddress2" value ="193.167.89.0 to 193.167.89.50"/>
 </appSettings>


and I want to check this ip address with user entered ip address. and In my frontend
I am using button function as

C#
string RestrictIP = ConfigurationManager.AppSettings["RestrictIPAddress"];
       string RestrictIP1 = ConfigurationManager.AppSettings["RestrictIPAddress1"];
       string RestrictIP2 = ConfigurationManager.AppSettings["RestrictIPAddress2"];

           if ((txtip.Text == RestrictIP1 || txtip.Text ==RestrictIP2 || txtip .Text ==RestrictIP ) && txtuname.Text == "admin" && txtpwd.Text == "admin")
           {
               lblmsg.Text = "your login successfully";
               Response.Redirect("welcome.aspx");
           }
           else
           {
               lblmsg.Text = "your IP address is not registered";
           }


It checks single IP but it does not support for other list.
Is that correct to use "to" in value attribute.

And Please Help me that how Can I access those values?

Thanks in advance.
Posted

1 solution

Hello ,

1) First of all Use The Following Function to Create List OF IP ADRESSES B GIving range oF IP's fron Web.config.

C#
private static ArrayList GetIpAdressFromRange(string fromIp, string toIp)
    {
        ArrayList ips = new ArrayList();
        IPAddress tmpIP = null;

        //generate only for valid set of ips
        //Todo: check for toip >fromIp
        if (IPAddress.TryParse(fromIp, out tmpIP) == true &&
                IPAddress.TryParse(toIp, out tmpIP) == true)
        {


            string[] fromOct = fromIp.Split('.');

            string[] toOct = toIp.Split('.');

            string a = null, b = null, c = null, d = null;

            int startA = Convert.ToInt32(fromOct[0]);
            int startB = Convert.ToInt32(fromOct[1]);
            int startC = 1;
            int startD = 1;

            int endA = Convert.ToInt32(toOct[0]);
            int endB = 255;
            int endC = 255;
            int endD = 255;


            for (int intA = startA; intA <= endA; intA++)
            {
                a = intA.ToString();

                startB = intA == Convert.ToInt32(fromOct[0]) ? Convert.ToInt32(fromOct[1]) : 1;
                endB = intA == Convert.ToInt32(toOct[0]) ? Convert.ToInt32(toOct[1]) : 255;

                for (int intB = startB; intB <= endB; intB++)
                {
                    b = intB.ToString();

                    startC = (intA == Convert.ToInt32(fromOct[0])) && (intB == Convert.ToInt32(fromOct[1])) ?
                        Convert.ToInt32(fromOct[2]) : 1;
                    endC = (intA == Convert.ToInt32(toOct[0])) && (intB == Convert.ToInt32(toOct[1])) ?
                        Convert.ToInt32(toOct[2]) : 255;


                    for (int intC = startC; intC <= endC; intC++)
                    {
                        c = intC.ToString();

                        startD = (intA == Convert.ToInt32(fromOct[0])) && (intB == Convert.ToInt32(fromOct[1])) && (intC == Convert.ToInt32(fromOct[2])) ?
                            Convert.ToInt32(fromOct[3]) : 1;
                        endD = (intA == Convert.ToInt32(toOct[0])) && (intB == Convert.ToInt32(toOct[1])) && (intC == Convert.ToInt32(toOct[2])) ?
                            Convert.ToInt32(toOct[3]) : 255;


                        for (int intD = startD; intD <= endD; intD++)
                        {
                            d = intD.ToString();
                            ips.Add(a + "." + b + "." + c + "." + d);
                            Console.WriteLine(a + "." + b + "." + c + "." + d);
                        }
                    }
                }
            }


        }
        return ips;

    }


2)Once You Will Get The Arralist OF IP adresseses that Ate Restricted you can Just Search in that list Wheather Provided IP IS restricted.

Refer Following Link For More Info ...
http://www.technosrix.com/2009/05/how-to-generate-ip-addresses-from-given.html[^]
 
Share this answer
 
Comments
iyalarasi 3-Oct-12 8:20am    
but I want that ipaddress list in webconfig file only?
I want to split configurationmanager.appsettings["RestrictIPAddress"].split();
It may be done in vb.net as
Split(ConfigurationManager.AppSettings("RestrictIPAddress"), "to")

but I want this to be done in an c# coding.

Anyway Thank you very much.
_Tushar Patil 3-Oct-12 8:50am    
According to your code you are putting Range in Web.config?
in this range you have to search whether users ip is present ?
iyalarasi 4-Oct-12 0:07am    
yes. I want to check user ip within that range or not?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900