• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1778
  • Last Modified:

ASP.NET web page to detect users IP, then redirect to another web page based on that

Hello,
I'm looking for a working default.aspx/default.aspx.cs in ASP.NET for a page which gets the visiting users IP and if it falls within these IP range the user is redirected to a URL, if it falls outside of these IP range then the user is forwarded to a different URL.

Get Users IP

Is the Users IP in the range e.g. 10.1.0.0 to 10.6.255.255

If yes, redirect to http://www.yahoo.com

else

redirect to http://www.msn.com

Any help with this will be appreciated, i'm useless at programming and need all the help i can get ASAP.

Thanks!
0
jeddi2k
Asked:
jeddi2k
  • 4
  • 2
1 Solution
 
mcgantsCommented:
at the very top of your code, add:
imports system.text.regularexpressions

then in page_load:

Dim userIPs String = HttpContext.Current.Request.UserHostAddress

if regex.ismatch(userIPs , "\b10\.[1-6]\.\d{1,3}\.\d{1,3}\b")
then
response.redirect("http://www.yahoo.com")
else
response.redirect("http://www.msn.com")
end if

That should work,
mcg
0
 
REA_ANDREWCommented:
here you go I just made this for you. It is C# and 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;
        }

     
    }


}
0
 
jeddi2kAuthor Commented:
Can you please send me a file called default.aspx that does this? that would just be much easier for me since I don know how to do it... please just make the file and send it to me. email it to me at jeddi2k@gmx.net. if it then works you get 500 credits. THANKS in advance...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
REA_ANDREWCommented:
that is the code behind page and here is the default.aspx page (We are not allowed to email material, has to be posted at all times)

You do not need anything on this page as it is just for redirecting, but here is the page anyway. Just change the values in the code above which is the default.aspx.cs page, i.e. the code behind page. Here is the default.aspx page

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Tests</title>
</head>
<body>

    <form id="form1" runat="server">
   
    </form>

</body>
</html>

0
 
REA_ANDREWCommented:
Now you have two files

default.aspx.cs
default.aspx

default.aspx.cs is the code I posted you

default.aspx is the blank page that is in my LAST post

thats it. I tested this and it works a treat
0
 
jeddi2kAuthor Commented:
Thank you so much!!!
You did a great job..
0
 
REA_ANDREWCommented:
no problem, glad to help. I have never done an IP range Validator before, so I enjoyed this :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now