【问题标题】:IP Address and Domain Restrictions in ASP.NETASP.NET 中的 IP 地址和域限制
【发布时间】:2015-07-31 07:15:31
【问题描述】:

我希望我的 Web 应用程序只能从确切的 IP 范围访问。

如果客户端IP不在WEB中保存的范围内。配置应用程序必须拒绝我对页面的访问。

【问题讨论】:

  • IIS 已经支持这样做。有什么理由在这里重新发明*?
  • 在 AZURE 网站中,您不能在 IIS 中进行任何更改。它不是服务器。它更类似于从面板管理的共享主机。所以我不是在重新发明*。刚刚分享了我的解决方案。

标签: asp.net azure web-config azure-web-app-service ip-restrictions


【解决方案1】:

那么我们如何将这些 IP 范围添加到 Web.config 中

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="IP" value="31.171.126.0/255,31.171.127.0/255"/>
  </appSettings>
  <system.web>
    <customErrors mode="Off"/>
    <compilation debug="true"/>
    <authentication mode="None"/>
  </system.web>
</configuration>

代码背后的工作原理:

protected void Page_Load(object sender, EventArgs e)
{
    var allowed = false;

    //Get IP ranges from Web.config
    // AS you see in web.config different IP ranges is seperated by comma

    var ip = ConfigurationManager.AppSettings["IP"];

    // Get Client Ip

    lblIp.Text = GetIpAddress().Split(':')[0];


    var clientIp = GetIpAddress();
    var list = ip.Split(',');


    //Do search inside IP ranges and see if client IP is inside IP ranges which is allowed to open web app. 
    foreach (var item in list)
    {
        var range = Convert.ToInt32(item.Split('/')[1].ToString(CultureInfo.InvariantCulture));

        for (var i=0; i <= range; i++)
        {

            var submaskip = item.Split('/')[0].Split('.')[0] + "." + item.Split('/')[0].Split('.')[1] + "." +
                            item.Split('/')[0].Split('.')[2] + "." + i;

            if (clientIp == submaskip)
            {
                allowed = true;
            }
        }

    }

    if (allowed == false)
    {
        Response.Redirect("Denied.aspx");
    }
}


// Get Client IP
protected string GetIpAddress()
{
    var context = System.Web.HttpContext.Current;
    var ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (string.IsNullOrEmpty(ipAddress)) return context.Request.ServerVariables["REMOTE_ADDR"];
    var addresses = ipAddress.Split(',');
    return addresses.Length != 0 ? addresses[0] : context.Request.ServerVariables["REMOTE_ADDR"];
}

【讨论】:

  • 最好将其移出页面逻辑并移入全局应用程序类 (global.asax) 的功能之一,因为该逻辑与页面无关。
  • 让 IIS 为您完成这项工作可能会更好。
  • 我们谈论的是 azure 网站。您没有机会在 IIS 设置中执行任何操作。
  • @RASKOLNIKOV 我曾经这么认为 - blogs.msdn.microsoft.com/benjaminperkins/2016/03/02/…
【解决方案2】:

Azure Web 应用程序(以前的 Azure 网站)支持此功能已有一段时间了。它是 IIS 的一项功能,Azure Web Apps 通过将 ipSecurity 元素添加到 web.config 使其可用。您不需要编写任何代码来执行此操作。

这是描述 Azure Web 应用程序功能的博客以及如何将配置添加到 web.config 的示例。

http://azure.microsoft.com/blog/2013/12/09/ip-and-domain-restrictions-for-windows-azure-web-sites/

【讨论】:

  • 请在共享 2013 年的链接之前阅读所有 cmets。可能当时在 AZURE 网站中 IIS 是 7.5 .NOT 8。我尝试了与您共享的相同的东西,在 IIS 8 中它不起作用。这就是为什么我可能分享了这个解决方案,有人也在寻找可行的解决方案。此外,我从未告诉过这只是解决方案和最佳解决方案。它只是解决方案。
  • @RASKOLNIKOV,我知道 cmets 并且是的,这确实有效。不确定您的情况,但我个人多次使用此功能,可以说它确实有效。编写代码来限制客户端 IP 并不是一个好主意,因为客户端已经进入您的应用程序。由于配置文件中的 IP 限制,服务器/IIS 8 会为您解决这个问题。
  • 尝试了基于 IP 地址或域名的访问限制,如所述。目前它在 Azure 中不起作用。试图限制 azure web 应用程序,使它们只能相互访问。