我有asp.net网络应用程序,我希望人们只能从我的公司网络(LAN)使用它,我该怎么做?
答案 0 :(得分:3)
你可以
要检查ips,请在BeginRequest上阅读客户端IP,检查它是in a range of the private networs还是放置其他你喜欢的ips,如果不是你只是关闭连接:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if(!IsPrivateNetwork(MyCurrentContent.Request.ServerVariables["REMOTE_ADDR"])
{
HttpContext.Current.Response.TrySkipIisCustomErrors = true;
HttpContext.Current.Response.StatusCode = 403;
HttpContext.Current.Response.End();
return ;
}
}
public bool IsPrivateNetwork(string sMyIpString)
{
long TheIpTranslate = AddressToNum(sMyIpString);
if (TheIpTranslate >= 0)
{
// 192.168.0.0 192.168.255.255
if (TheIpTranslate >= 3232235520 && TheIpTranslate <= 3232301055)
return true;
// 10.0.0.0 10.255.255.255
if (TheIpTranslate >= 167772160 && TheIpTranslate <= 184549375)
return true;
// 172.16.0.0 172.31.255.255
if (TheIpTranslate >= 2886729728 && TheIpTranslate <= 2887778303)
return true;
}
return false;
}
public long AddressToNum(string cAddress)
{
IPAddress MyIpToCheck = null;
if (IPAddress.TryParse(cAddress, out MyIpToCheck))
{
return AddressToNum(MyIpToCheck);
}
else
{
return -1;
}
}
public long AddressToNum(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;
}