Check IP Range

Check IP Range using asp.net code sample




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IpBlocker.IPCheckers
{
public class IpRangeChecker : IChcker
{
public string[] IpRanges { get; set; }
public string Access { get; set; }

public IpRangeChecker(string value, string access)
{
this.IpRanges = value.Split(',');
this.Access = access;
}

#region IChcker Members

public bool CanBlockIp(string ipAddress)
{
bool result = false;
foreach (string ipRange in IpRanges)
{
string[] ipValues = ipRange.Split(';');
if (string.Compare(this.Access, "granted", true) == 0)
{
if ((IpCompare.IsGreaterOrEqual(ipAddress, ipValues[0])) &&
(IpCompare.IsLessOrEqual(ipAddress, ipValues[1])))
{
result = false;
break;
}
else
{
result = true;
}
}
else if (string.Compare(this.Access, "denied", true) == 0)
{
if ((IpCompare.IsGreaterOrEqual(ipAddress, ipValues[0])) &&
(IpCompare.IsLessOrEqual(ipAddress, ipValues[1])))
{
result = true;
break;
}
else
{
result = false;
}
}
else
{
throw new ArgumentException(string.Format("Invalid Access", this.Access));
}
}

return result;
}

#endregion
}
}


Comments

Author: Phagu Mahato10 Feb 2014 Member Level: Gold   Points : 3

You can apply this Method that gets IP address: ASP.NET, C#


using System;
using System.Web;

namespace WebApplication1
{
public class Global : HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{

HttpRequest request = base.Request;


string myaddress = request.UserHostAddress;


base.Response.Write(myaddress);


base.CompleteRequest();
}
}
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: