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 VP开发者_StackOverflowN connection.
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.
精彩评论