views:

54

answers:

1

This is regarding ASP.NET MVC. Let's say I have a web site that is accessed publicly name as "www.welcomeeveryone.com". There is also a web site name as "www.companyemployeeonly.com" that is allowed only for certain range of IP address. Employees have to go to "www.welcomeeveryone.com" first in order to have a link for "www.companyemployeeonly.com". Here is my question. How do I detect the user is in the allowed IP or not and give them a warning like popup or some kind of notification. When they are not in the certain IP address, then they have to use VPN connection.

+1  A: 

You can get the remote IP using

Request.ServerVariables["REMOTE_HOST"]

Then converting to IpAddress and then you can use this functions to convert it to long and make the compare

public long addrToNum(IPAddress Address)
{
    byte[] b = BitConverter.GetBytes(Address.Address);

    if (b.Length == 8)
        return (long)(((long)16777216 * b[0]) + ((long)(65536 * b[1])) + ((long)(256 * b[2])) + b[3]);
    else
        return 0;
}

Be ware that the Remore_Host is not valid if he is behind proxy, and you need to discover that, and also can be hacked.

Aristos
What do you mean by "can be hacked"? Who might be hacked?
Hoorayo
@hoorayo This is a parameter that can be send by the user with different ip than the real one. Its not 100% accurate.
Aristos