IPValidator.cs
5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
namespace AIAHTML5.API.Models
{
public class IPValidator
{
public static Double ChangeIpToDouble(String ipVal)
{
String[] resultArray = new String[20];
//Array resultArray ;
String n = ipVal;
String[] array = n.Split('.');
for (int i = 0; i < array.Length; i++)
{
if (array[i].Length == 1)
{
array[i] = "00" + array[i];
resultArray[i] = (array[i]);
}
else if (array[i].Length == 2)
{
array[i] = "0" + array[i];
resultArray[i] = (array[i]);
}
else
{
array[i] = array[i];
resultArray[i] = (array[i]);
}
}
//String theContent = resultArray.ToString();
String theContent = "";
for (int i = 0; i <= resultArray.Length - 1; i++)
{
if (!String.IsNullOrEmpty(resultArray[i]))
theContent += resultArray[i];
}
theContent = (theContent == "" ? "0" : theContent);
return Convert.ToDouble(theContent);
//return theContent;
}
public static bool ValidateIP(String strSiteIp, String strSiteIPTo, String strSiteMasterIPTo, String strIPToValidate, int intIsMaster)
{
Double dblSiteIP = 0;
Double dblSiteIPTo = 0;
Double dblSiteMIPTo = 0;
Double dblIPToValidate = 0;
bool boolReturn = false;
try
{
// Convert all IP to double values
dblSiteIP = ChangeIpToDouble(((strSiteIp == "" || IsNumericIP(strSiteIp) == false) ? "0" : strSiteIp));
dblSiteIPTo = ChangeIpToDouble(((strSiteIPTo == "" || IsNumericIP(strSiteIPTo) == false) ? "0" : strSiteIPTo));
dblSiteMIPTo = ChangeIpToDouble(((strSiteMasterIPTo == "" || IsNumericIP(strSiteMasterIPTo) == false) ? "0" : strSiteMasterIPTo));
dblIPToValidate = ChangeIpToDouble(((strIPToValidate == "" || IsNumericIP(strIPToValidate) == false) ? "0" : strIPToValidate));
if (intIsMaster > 0)
{
if (dblSiteIP == dblIPToValidate)
boolReturn = true;
if (dblSiteIPTo > 0 && dblSiteMIPTo > 0)
{
if (isBetween(dblIPToValidate, dblSiteIPTo, dblSiteMIPTo) == true)
{
boolReturn = true;
}
}
else if (dblSiteIPTo > 0)
{
if (dblSiteIPTo == dblIPToValidate)
boolReturn = true;
}
else if (dblSiteMIPTo > 0)
{
if (dblSiteMIPTo == dblIPToValidate)
boolReturn = true;
}
}
else
{
if (dblSiteIP > 0 && dblSiteIPTo > 0)
{
if (isBetween(dblIPToValidate, dblSiteIP, dblSiteIPTo) == true)
{
boolReturn = true;
}
}
else if (dblSiteIP > 0)
{
if (dblSiteIP == dblIPToValidate)
boolReturn = true;
}
else if (dblSiteIPTo > 0)
{
if (dblSiteIPTo == dblIPToValidate)
boolReturn = true;
}
}
return boolReturn;
}
catch (Exception objExp)
{
return false;
}
}
public static String FormatURLToIP(String strURL)
{
strURL = strURL.Replace("www.", "");
strURL = strURL.Replace("http://", "");
strURL = strURL.Replace("https://", "");
strURL = strURL.Replace("/", "");
if (strURL.IndexOf(":") != -1)
{
char[] delimiters = new char[] { ':' };
string[] parts = strURL.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
strURL = parts[0];
}
return strURL;
}
/// <summary>
///
/// </summary>
/// <param name="iNum"></param>
/// <param name="iFrom"></param>
/// <param name="iTo"></param>
/// <returns></returns>
private static bool isBetween(Double iNum, Double iFrom, Double iTo)
{
if (iNum == 0)
return false;
if (iNum >= iFrom && iNum <= iTo)
{
return true;
}
return false;
}
/// <summary>
///
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static bool IsNumericIP(string text)
{
//Regex objRegex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
Regex objRegex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$*\.?[0-9]+$*\.?[0-9]+$*\.?[0-9]+$");
return objRegex.IsMatch(text);
}
}
}