使用asp.net中的web.config中的ipaddress列表检查ip地址 [英] checking ip address with the list of ipaddress in web.config in asp.net

查看:104
本文介绍了使用asp.net中的web.config中的ipaddress列表检查ip地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Webconfig文件中的IP地址列表作为

I am using list of ip address in my webconfig file as

<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>



我想用用户输入的IP地址检查该IP地址.并在我的前端
我正在将按钮功能用作



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

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";
           }



它检查单个IP,但不支持其他列表.
在value属性中使用"to"是否正确.

并且请帮助我,我该如何访问这些值?

在此先谢谢您.



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.

推荐答案

您好,

1)首先使用以下功能创建IP地址列表,以从IP的Web.config中分配IP地址.

Hello ,

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

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)一旦您获得了Ate限制的IP地址专家,就可以在该列表中搜索Wheather提供的IP IS受到限制.

请参阅以下链接以获取更多信息...
http://www.technosrix.com/2009/05/how-to-generate-ip-addresses-from-given.html [



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[^]


这篇关于使用asp.net中的web.config中的ipaddress列表检查ip地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆