gpt4 book ai didi

asp.net - 我可以通过编程方式从 ASP.NET 应用程序将 IP 地址添加到 IIS7 中的动态 IP 限制扩展吗?

转载 作者:行者123 更新时间:2023-12-02 21:13:41 24 4
gpt4 key购买 nike

我正在创建一个基于论坛的网站,并希望阻止发布垃圾邮件或滥用行为的成员。我正在考虑使用 HTTPModule 来执行此操作,但我遇到了 IIS7 的动态 IP 限制扩展。我想知道是否可以将 IP 从我的应用程序动态添加到扩展程序?

另外,如果您有该扩展的经验,那就太好了。我特别是。有兴趣知道它是否会影响高流量网站的性能。

谢谢

最佳答案

我也对此感兴趣。

起初我使用 IIS7 中的 UI 将 IP 地址列入黑名单。

enter image description here

我确实查看了上面提到的 Rick Strahl 链接,但在这里找到了一个很棒的资源:

http://www.iis.net/configreference/system.webserver/security/ipsecurity/add

该页面上的代码示例向您展示了如何使用 C# 执行该操作。这是该网站的片段

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
private static void Main()
{
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity", "Default Web Site");
ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection();

ConfigurationElement addElement = ipSecurityCollection.CreateElement("add");
addElement["ipAddress"] = @"192.168.100.1";
addElement["allowed"] = false;
ipSecurityCollection.Add(addElement);

ConfigurationElement addElement1 = ipSecurityCollection.CreateElement("add");
addElement1["ipAddress"] = @"169.254.0.0";
addElement1["subnetMask"] = @"255.255.0.0";
addElement1["allowed"] = false;
ipSecurityCollection.Add(addElement1);

serverManager.CommitChanges();
}
}
}

要获取 Microsoft.Web.Administration 包,请在 Visual Studio 中转到“工具”->“Nuget 包管理器”->“包管理器控制台”。

然后输入:

Install-Package Microsoft.Web.Administration

执行相同任务的另一种方法是使用命令行和 appcmd 命令。

以下命令执行相同的操作:

appcmd.exe set config "Default Web Site/SSM" -section:system.webServer/security/ipSecurity /+"[ipAddress='192.168.100.1',allowed='False']" /commit:apphost

并且可以使用以下代码从代码中调用:

string website = "Default Web Site/SSM";
string ipAddress = "192.168.100.1";
string allowDeny = "False";

string cmd = string.Format("%systemroot%\\system32\\inetsrv\\appcmd.exe set config \"{0}\" -section:system.webServer/security/ipSecurity /+\"[ipAddress='{1}',allowed='{2}']\" /commit:apphost", website, ipAddress, allowDeny);
Process.Start(cmd);

上面的命令有效,但事实证明,如果您从 C# 调用它,它会提示“系统找不到指定的文件异常”。要解决这个问题,您必须提供管理员用户名/密码。

这是函数:

void BlacklistIP(string ipAddress)
{
string website = "Default Web Site/SSM";
string allowDeny = "False";
string domain = "";

string args = string.Format(" set config \"{0}\" -section:system.webServer/security/ipSecurity /+\"[ipAddress='{1}',allowed='{2}']\" /commit:apphost", website, ipAddress, allowDeny);

System.Security.SecureString password = new System.Security.SecureString();
password.AppendChar('y');
password.AppendChar('o');
password.AppendChar('u');
password.AppendChar('r');
password.AppendChar('p');
password.AppendChar('a');
password.AppendChar('s');
password.AppendChar('s');
password.AppendChar('w');
password.AppendChar('o');
password.AppendChar('r');
password.AppendChar('d');

Process.Start(@"C:\windows\System32\inetsrv\appcmd.exe", args, "Administrator", password, domain);
}

瞧!

关于asp.net - 我可以通过编程方式从 ASP.NET 应用程序将 IP 地址添加到 IIS7 中的动态 IP 限制扩展吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/589851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com