我们有一个 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
,您需要将以下设置添加到您的角色:
设置的语法并不难理解:
IPAddressRestriction.Enabled = true
或 false
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/