Link to home
Start Free TrialLog in
Avatar of jeddi2k
jeddi2k

asked on

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!
Avatar of mcgants
mcgants

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
ASKER CERTIFIED SOLUTION
Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jeddi2k

ASKER

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...
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>

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
Avatar of jeddi2k

ASKER

Thank you so much!!!
You did a great job..
no problem, glad to help. I have never done an IP range Validator before, so I enjoyed this :-)