I need to redirect some users based on their ip address to specific page else they would be redirected to another page my code is as below.. please advice... why its not working... thanks for help...
Hi Good day. All you have to do is change the MaxIp in the Range, the Min IP in the range and the two addresses it redirects to dependant on the IP
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Sockets;
hyme
Member
4 Points
13 Posts
Redirect Based on IP Address
Feb 02, 2010 09:17 PM|LINK
I need to redirect some users based on their ip address to specific page else they would be redirected to another page my code is as below.. please advice... why its not working... thanks for help...
string myIP = HttpContext.Current.Request.UserHostAddress.ToString();
switch(myIP)
{
case "10.10.10.0":
Response.Redirect("http://www.yahoo.com");
break;
default:
Response.Redirect(http://www.google.com);
break;
}
crajeshbe
Participant
1818 Points
300 Posts
Re: Redirect Based on IP Address
Feb 03, 2010 03:23 AM|LINK
Hi Good day. All you have to do is change the MaxIp in the Range, the Min IP in the range and the two addresses it redirects to dependant on the IP
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net.NetworkInformation;
using System.Net.Sockets;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string MaxIp="80.0.0.10";
string MinIp="80.0.0.1";
string UserIP = Request.ServerVariables["REMOTE_ADDR"].ToString();
if (GetIP(UserIP) >= GetIP(MinIp) && GetIP(UserIP) <= GetIP(MaxIp))
{
Response.Redirect("Correct.aspx");
}
else
{
Response.Redirect("http://www.msn.com");
}
}
protected long GetIP(string IpTarget)
{
try{
string Constructor = "";
string[] IpSplit = IpTarget.Split(Convert.ToChar("."));
for (int i = 0; i < IpSplit.Length; i++)
{
if (IpSplit[i].Length < 2)
{
IpSplit[i] = "00" + IpSplit[i];
}
else if (IpSplit[i].Length < 3 && IpSplit[i].Length > 1)
{
IpSplit[i] = "0" + IpSplit[i];
}
Constructor += IpSplit[i].ToString();
}
return Convert.ToInt64(Constructor);
}
catch(Exception IPex)
{
return 0;
}
}
}
Software Engineer MCPD - (ASP.NET Developer 3.5 & Windows Developer 3.5)
My website
View my MCP Certifications
Don't forget to Mark as ANSWER
qwe123kids
All-Star
48619 Points
7957 Posts
MVP
Re: Redirect Based on IP Address
Feb 03, 2010 03:25 AM|LINK
Hi,
Just Response.write(myIP )
Response.end()
you will get what ip Ur gettig and Chk it they Statisfy the the switch case
OR
Other way finding Ip address...
Request.ServerVariables("REMOTE_ADDR")
Request.ServerVariables ( "HTTP_X_FORWARDED_FOR" )
chk the abov lionk
Avinash Tiwari
Remember to click “Mark as Answer” on the post, if it helps you.