asp.net - 动态阻止 Azure 云服务的 IP 地址

标签 asp.net azure ip azure-web-app-service

我们有一个 Azure 云服务(经典),它是我们的 Web 应用程序。我们如何告诉服务阻止所有实例上的特定 IP?

我知道当通过 web.config 文件设置时,我们可以使用 IIS 8+ 动态 IP 限制 (DIPR) 进行阻止。 Configuring Dynamic IP Restrictions

这样做的两个问题是 1) 我们无法从应用程序内添加到始终阻止列表中,2) 即使我可以让它工作,它也只能在该实例上。

没有办法阻止/IP 过滤来自门户的流量吗? 可以在我们的云服务中设置吗?

最佳答案

Is there no way to block/IP Filter traffic from the portal? And can it be set from within our Cloud Service?

每次启动实例时,它都会查看已为角色配置的端点,并在防火墙中打开所需的端口。我们在代码中要做的只是禁用这些规则并创建仅限于少数 IP 地址/IP 地址范围的新规则

此解决方案的核心是 IPAddressRestrictionManager.cs 类,它解析 ServiceConfiguration.cscfg 中的设置(您可以在部署应用程序时对其进行修改)并修改每个实例上的防火墙。

首先您需要安装 NuGet 包:

PM> Install-Package WindowsAzure.IPAddressRestriction

如果您想将 IPAddressRestrictionManager 链接到 ServiceConfiguration,您需要将以下设置添加到您的角色:

enter image description here

设置的语法并不难理解:

IPAddressRestriction.Enabled = truefalse

IPAddressRestriction.Settings = = 或 =-(端口之间的分隔符为“;”)

最后,您需要将 WebRole/WorkerRoler.cs 中的所有内容连接起来

public class WebRole : RoleEntryPoint  
{
    private IPAddressRestrictionManager restrictionManager;

    public override bool OnStart()
    {
        RoleEnvironment.Changing += OnRoleEnvironmentChanging;
        ConfigureIPAddressRestrictions();
        return base.OnStart();
    }

    private void ConfigureIPAddressRestrictions()
    {
        if (restrictionManager == null)
            restrictionManager = new IPAddressRestrictionManager();

        restrictionManager.RemoveRestrictions();
        if (restrictionManager.IsEnabledInConfiguration())
            restrictionManager.ApplyFromConfiguration();
    }

    /// <summary>
    /// Force restart of the instance.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void OnRoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
    {
        if (e.Changes.Any(o => o is RoleEnvironmentChange))
            e.Cancel = true;
    }
}

更多详情可以引用这个article .

关于asp.net - 动态阻止 Azure 云服务的 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52726839/

相关文章:

c# - asp.net 中的 Javascript 验证错误

Asp.net LoginStatus - 'log out' 显示但尚未登录

azure - 如何在 Azure 资源图 kusto 查询的输出中反射(reflect)层次结构?

python - Ubuntu 上的 Azure CLI 权限被拒绝错误

azure - Azure AD OAuth2 中的refresh_token 是否过期

c# - 在与请求匹配的 Controller 上未找到任何操作

asp.net - Ajax UpdateProgress 在使用 Response.Redirect 后不会停止旋转

wordpress - 将域名指向运行 XAMPP 和 wordpress 的服务器 ip

python - scapy "TypeError: ' IP'对象不可调用”

c - 创建服务器/客户端 TCP/IP 程序时遇到问题