Click here to Skip to main content
15,887,812 members
Home / Discussions / C#
   

C#

 
GeneralRe: Representing a 4 digit integer in 2 positions alphanumeric placeholder. Pin
BobJanova17-Jul-12 0:12
BobJanova17-Jul-12 0:12 
Generalamd Pin
sayem_sam16-Jul-12 2:18
sayem_sam16-Jul-12 2:18 
AnswerRe: a module Pin
Wes Aday16-Jul-12 2:22
professionalWes Aday16-Jul-12 2:22 
AnswerRe: a module Pin
Luc Pattyn16-Jul-12 4:02
sitebuilderLuc Pattyn16-Jul-12 4:02 
GeneralMessage Removed Pin
16-Jul-12 4:11
professionalN_tro_P16-Jul-12 4:11 
AnswerRe: a module Pin
Luc Pattyn16-Jul-12 4:57
sitebuilderLuc Pattyn16-Jul-12 4:57 
GeneralRe: a module Pin
Wes Aday16-Jul-12 5:40
professionalWes Aday16-Jul-12 5:40 
GeneralRe: a module Pin
Pete O'Hanlon16-Jul-12 4:34
mvePete O'Hanlon16-Jul-12 4:34 
If I were you, I would ditch the commented out lines. I would also look into using string.IsNullOrEmpty (or string.IsNullOrWhitespace if you are using .NET 4).

As you've removed your code, here it is for anyone who wonders what my comments are about:
C#
using System;
using System.Web;
using System.Xml;
 

/// 
/// HTTP module to restrict access by IP address
/// 
public class SecurityHttpModule : IHttpModule
{
public SecurityHttpModule() { }
/// 
/// Module are initialize here by using public Init function, that does not return a value;
/// HttpApplication: defines the methode properties and events that are common to all application object;
/// 
/// 
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Application_BeginRequest);
}
/// 
/// 
/// 
/// 
/// 
private void Application_BeginRequest(object source, EventArgs e)
{
HttpContext context = ((HttpApplication)source).Context;
string ipAddress = context.Request.UserHostAddress;
//string ipAddress = "202.183.79.250";//ok ip for test
//string ipAddress = "202.183.64.0";//ok ip for test
// string st="";
// string st1 = "";
string strIPAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIPAddress == null || strIPAddress == "")
{
//string[] myservab = context.Request.ServerVariables.AllKeys;
//for (int i = 0; i <= myservab.Length; i++)
//{
// //respons.Write("Key: " + myservab[i] + "
");

// st += " Key: " + myservab[i] + " 
" ;
// string[] myservab2 = context.Request.ServerVariables.GetValues(myservab[i]);
// for (int j = 0; j < myservab2.Length; j++)
// {
// st1 += "Key : " + myservab[i] + " Value : " + myservab2[j] + "
";
// //Response.Write("Value " + myservab2 + ": " + Server.HtmlEncode(myservab2[j]) + "
");
// }
 
//}
strIPAddress = context.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
if (!IsValidIpAddress(ipAddress))
{
context.Response.StatusCode = 403; // (Forbidden)
}
else
{
string filePath = context.Request.FilePath;
string fileExtension =
VirtualPathUtility.GetExtension(filePath);
if (fileExtension.Equals(".aspx"))
{
context.Response.Write("
" +
"HelloWorldModule: Beginning of Request" +
"

--------------------------------------------------------------------------------
");
}
}
}
 
private bool IsValidIpAddress(string ipAddress)
{
 
double myipnom = IPAddressToNumber(ipAddress);
//long myipnom = ip2ipno(ipAddress);
// long myipra = 3401007104;
string myip = ipno2ipaddress(myipnom);
bool sta = false;
Class1 obj = new Class1();
XmlDocument xmldoc = obj.getdata(myipnom);
//xmldoc.InnerXml
// XmlNodeList xmlist = xmldoc.SelectNodes("CountryIps[@*]");
XmlNodeList xmlist = xmldoc.GetElementsByTagName("CountryIps");
 
// XmlNodeList xmlist = xmldoc.InnerXml("NewDataSet[@*]");
//XmlNodeList xmll=xmldoc.InnerXml
 
foreach (XmlNode nl in xmlist)
{
if (nl != null)
{
// XmlNode tablexml = nl.SelectSingleNode("id");
string ipFrom = nl["ipFROM"].InnerXml;
string ipTo = nl["ipTO"].InnerXml;
string countrySHORT = nl["countrySHORT"].InnerXml;
string countryLONG = nl["countryLONG"].InnerXml;
bool conf;
if (ipFrom != null && ipTo != null)
{
conf = IsInRange(myipnom, double.Parse(ipFrom), double.Parse(ipTo));
if (conf == true && countrySHORT=="IN")
{
sta = true;
}
else { sta = false; }
}
}
else
{
sta = false;
}
 
}
return sta;
// return (ipAddress == "127.0.0.1");
}
public bool IsInRange(double ipNo, double ipFrom, double ipTo)
{
Int64 ipf= Convert.ToInt64(ipFrom);
Int64 ipn = Convert.ToInt64(ipNo);
Int64 ipt = Convert.ToInt64(ipTo);
return ipn >= ipf && ipn <= ipt;
}
public long ip2ipno(string ipAddress)
{
char[] splitchar = { '.' };
string[] strary = ipAddress.Split(splitchar);
long ipNo;
double ipno1;
double ipNumber, ipNumber1, ipNumber2, ipNumber3;
ipNumber = (Math.Pow(2, 24) * int.Parse(strary[0]));
ipNumber1 = (Math.Pow(2, 16) * int.Parse(strary[1]));
ipNumber2 = (Math.Pow(2, 8) * int.Parse(strary[2]));
ipNumber3 = int.Parse(strary[3]);
ipno1 = (ipNumber + ipNumber1+ ipNumber2 + ipNumber3);
ipNo = (Convert.ToInt64(ipNumber) + Convert.ToInt64( ipNumber1 )+ Convert.ToInt32( ipNumber2 )+ Convert.ToInt32(ipNumber3));
return ipNo;
}
public double IPAddressToNumber(string IPaddress)
{
int i;
string[] arrDec;
double num = 0;
if (IPaddress == "")
{
return 0;
}
else
{
arrDec = IPaddress.Split('.');
for (i = arrDec.Length - 1; i >= 0; i = i - 1)
{
num += ((int.Parse(arrDec[i]) % 256) * Math.Pow(256, (3 - i)));
}
return num;
}
}
 
public string ipno2ipaddress(double ipNo)
{
double w, x, y, z;
w = (ipNo / 16777216) % 256;
x = (ipNo / 65536) % 256;
y = (ipNo / 256) % 256;
z = (ipNo) % 256;
int w1 = Convert.ToInt16(Math.Floor(w));
int x1 = Convert.ToInt16(Math.Floor(x));
int y1 = Convert.ToInt16(Math.Floor(y));
int z1 = Convert.ToInt16(Math.Floor(z));
//int w1 = Math.Floor(w);
return (w1)+ "." + Convert.ToInt32(x1) + "." + Convert.ToInt32(y1) + "." + Convert.ToInt32(z1);
}
public void Dispose() { /* clean up */ }
 
}
 
//XmlDocument xml = new XmlDocument();
//xml.LoadXml(myXmlString); //myXmlString is the xml file in string //copying xml to string: string myXmlString = xmldoc.OuterXml.ToString();
//XmlNodeList xnList = xml.SelectNodes("/Element[@*]/ANode/BNode/CNode");
//foreach (XmlNode xn in xnList)
//{
// XmlNode example = xn.SelectSingleNode("Example");
// if (example != null)
// {
// string na = example["Name"].InnerText;
// string no = example["NO"].InnerText;
// }
//}

*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

"Mind bleach! Send me mind bleach!" - Nagy Vilmos


CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

Questioncrystal report in C# using dataset and datatable Pin
rohitatcp15-Jul-12 21:07
rohitatcp15-Jul-12 21:07 
Question[SOLVED] GUI Problem - Added Forms To Tab Control Pin
AmbiguousName15-Jul-12 20:40
AmbiguousName15-Jul-12 20:40 
AnswerRe: [SOLVED] GUI Problem - Added Forms To Tab Control Pin
Obaid ur Rehman15-Jul-12 23:25
Obaid ur Rehman15-Jul-12 23:25 
AnswerRe: [SOLVED] GUI Problem - Added Forms To Tab Control Pin
AmbiguousName16-Jul-12 0:54
AmbiguousName16-Jul-12 0:54 
Generalwhat info is needed to detect a HtmlElement in WebBrowser Pin
linuor15-Jul-12 19:47
linuor15-Jul-12 19:47 
GeneralRe: what info is needed to detect a HtmlElement in WebBrowser Pin
Richard MacCutchan15-Jul-12 22:20
mveRichard MacCutchan15-Jul-12 22:20 
GeneralRe: what info is needed to detect a HtmlElement in WebBrowser Pin
linuor16-Jul-12 3:15
linuor16-Jul-12 3:15 
GeneralRe: what info is needed to detect a HtmlElement in WebBrowser Pin
Richard MacCutchan16-Jul-12 3:57
mveRichard MacCutchan16-Jul-12 3:57 
GeneralSerial Port Read Timeout Problem Pin
Richard Andrew x6415-Jul-12 18:21
professionalRichard Andrew x6415-Jul-12 18:21 
GeneralRe: Serial Port Read Timeout Problem Pin
Peter_in_278015-Jul-12 19:30
professionalPeter_in_278015-Jul-12 19:30 
GeneralRe: Serial Port Read Timeout Problem Pin
Richard Andrew x6415-Jul-12 19:55
professionalRichard Andrew x6415-Jul-12 19:55 
GeneralRe: Serial Port Read Timeout Problem Pin
SoMad15-Jul-12 20:45
professionalSoMad15-Jul-12 20:45 
GeneralRe: Serial Port Read Timeout Problem Pin
glennPattonWork315-Jul-12 22:38
professionalglennPattonWork315-Jul-12 22:38 
QuestionStart Debugger "from running program" Pin
Tomerland15-Jul-12 9:12
Tomerland15-Jul-12 9:12 
GeneralRe: Start Debugger "from running program" Pin
PIEBALDconsult15-Jul-12 9:23
mvePIEBALDconsult15-Jul-12 9:23 
AnswerRe: Start Debugger "from running program" Pin
Eddy Vluggen15-Jul-12 10:49
professionalEddy Vluggen15-Jul-12 10:49 
AnswerRe: Start Debugger "from running program" Pin
markovl15-Jul-12 23:06
markovl15-Jul-12 23:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.