gpt4 book ai didi

c# - 以编程方式编辑 IIS IPGrant 表

转载 作者:行者123 更新时间:2023-11-30 18:07:17 24 4
gpt4 key购买 nike

我一直致力于在 IIS 中编辑 IPGrant 表的编程解决方案。

就目前而言,我可以正确查看 IPGrant 列表,并可以添加到它。

但是,我无法删除或替换 IPGrant 列表中的项目。

MSDN 等建议您将(旧列表的值 + 新值)写入列表,但是我发现我得到的 HResult 是“无法使用该名称创建文件,文件已存在” .添加到列表仅适用于我如果我只传入新值。

阅读后:

http://www.west-wind.com/weblog/posts/59731.aspx
http://www.aspdev.org/articles/web.config/
http://www.codeproject.com/KB/security/iiswmi.aspx
http://www.codeproject.com/KB/security/iiswmi.aspx?msg=1739049
http://blogs.msdn.com/b/shawnfa/archive/0001/01/01/400749.aspx
http://msdn.microsoft.com/en-us/library/ms524322%28VS.90%29.aspx
http://www.eggheadcafe.com/software/aspnet/33215307/setting-ip-restrictions-in-iis-7.aspx

我发现 IIS 7/6 和使用元数据库存在兼容性问题 - 因为元数据库只能添加,不能删除。

IIS 7/7.5 是否有更新的方法可用于(请在 c# 中)管理 IPGrant 表。

最佳答案

您可以使用 Microsoft.Web.Administration、AppCmd 或 Javascript (COM - AHADMIN) 来执行此操作,以下是有关如何删除的几个示例:

private static void Main() {

using(ServerManager serverManager = new ServerManager()) {
Configuration config = serverManager.GetApplicationHostConfiguration();

ConfigurationSection ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity");

ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection();

ConfigurationElement addElement = FindElement(ipSecurityCollection, "add", "ipAddress", @"169.132.124.234", "subnetMask", @"255.255.255.255", "domainName", @"");
if (addElement == null) throw new InvalidOperationException("Element not found!");

ipSecurityCollection.Remove(addElement);

serverManager.CommitChanges();
}
}

private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues) {
foreach (ConfigurationElement element in collection) {
if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase)) {
bool matches = true;

for (int i = 0; i < keyValues.Length; i += 2) {
object o = element.GetAttributeValue(keyValues[i]);
string value = null;
if (o != null) {
value = o.ToString();
}

if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase)) {
matches = false;
break;
}
}
if (matches) {
return element;
}
}
}
return null;
}

使用 Javascript:

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";

var ipSecuritySection = adminManager.GetAdminSection("system.webServer/security/ipSecurity", "MACHINE/WEBROOT/APPHOST");

var ipSecurityCollection = ipSecuritySection.Collection;

var addElementPos = FindElement(ipSecurityCollection, "add", ["ipAddress", "169.132.124.234","subnetMask", "255.255.255.255","domainName", ""]);if (addElementPos == -1) 抛出“找不到元素!”;

ipSecurityCollection.DeleteElement(addElementPos);

adminManager.CommitChanges();

function FindElement(collection, elementTagName, valuesToMatch) { 对于 (var i = 0; i < collection.Count; i++) { var element = collection.Item(i);

    if (element.Name == elementTagName) {
var matches = true;
for (var iVal = 0; iVal < valuesToMatch.length; iVal += 2) {
var property = element.GetPropertyByName(valuesToMatch[iVal]);
var value = property.Value;
if (value != null) {
value = value.toString();
}
if (value != valuesToMatch[iVal + 1]) {
matches = false;
break;
}
}
if (matches) {
return i;
}
}
}

return -1;

最后是 AppCmd.exe:
appcmd.exe set config -section:system.webServer/security/ipSecurity/-[ipAddress='169.132.124.234',subnetMask='255.255.255.255',domainName='']"/commit:apphost

关于c# - 以编程方式编辑 IIS IPGrant 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4070066/

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