【问题标题】:Blocking IPs to All Apps But One [closed]阻止除一个以外的所有应用程序的 IP [关闭]
【发布时间】:2014-05-19 16:36:53
【问题描述】:

我需要允许美国用户连接到我们的客户网站(其中 10 个),但允许所有人访问我们的在线支持票务网站,因为我们在美国以外的技术人员只需要访问这个网站。

我们有一个运行 Windows Server 2008 R2 的专用服务器。

我打算运行一个脚本,只允许美国 IP 范围通过 Windows 防火墙,但后来我记得我们在同一台服务器上拥有票务站点,所以如果我继续执行脚本,我会阻止票务站点也是。

我怎样才能仍然只允许 10 个客户站点上的美国 IP 范围,但允许票务站点上的每个人?

我们使用的是 IIS 7.5。

谢谢

【问题讨论】:

  • 您确定这是正确的方法吗?有时,由于不是他们自己的过错,人们最终会通过(正确或错误地)“映射”到非美国位置的 IP 地址连接到站点。您确定要阻止此类用户使用您的网站吗? IP->位置映射是一种粗略的工具,我建议将它们用于例如默认语言/地区设置,但不适用于访问控制

标签: asp.net windows iis firewall


【解决方案1】:

您可以检查 BeginRequest 上的 Global.asax

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    // here check if is allowed
    if(!IsOnPermitedNetwork(MyCurrentContent.Request.ServerVariables["REMOTE_ADDR"])
    {
        HttpContext.Current.Response.TrySkipIisCustomErrors = true;
        HttpContext.Current.Response.StatusCode = 403;
        HttpContext.Current.Response.End();
        return ;    
    }
}

以及您需要进行检查的一些实用程序功能。

public bool IsOnPermitedNetwork(string sMyIpString)
{
    long TheIpTranslate = AddressToNum(sMyIpString);

    if (TheIpTranslate >= 0)
    {
        // exampple
        // 192.168.0.0 192.168.255.255
        if (TheIpTranslate >= 3232235520 && TheIpTranslate <= 3232301055)
            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;
}

【讨论】:

    猜你喜欢
    • 2016-11-06
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    相关资源
    最近更新 更多