【发布时间】:2022-01-17 01:15:08
【问题描述】:
使用以下代码,我能够将用户添加到我的 IIS 配置中的 OneToOneMappings 部分,但我该如何再次删除用户?
using System;
using System.Text;
using Microsoft.Web.Administration;
public class Sample
{
public static void Main()
{
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection iisClientCertificateMappingAuthenticationSection = config.GetSection("system.webServer/security/authentication/iisClientCertificateMappingAuthentication", "CertificateSite");
ConfigurationElementCollection oneToOneMappingsCollection = iisClientCertificateMappingAuthenticationSection.GetCollection("oneToOneMappings");
ConfigurationElement addElement = oneToOneMappingsCollection.CreateElement("add");
addElement["enabled"] = true;
addElement["userName"] = "banana";
addElement["password"] = "banana";
addElement["certificate"] = "banana";
oneToOneMappingsCollection.Add(addElement);
serverManager.CommitChanges();
}
}
}
我尝试将 ConfigurationElementCollection 打印到控制台以查看它,但它没有向我显示任何信息(我可能太笨而无法正确打印它)。我想要的是找到一个用户,然后从配置中删除那个“添加”元素,目前看起来像这样:
<configuration>
<location path="CertificateSite">
<system.webServer>
<security>
<authentication>
<iisClientCertificateMappingAuthentication enabled="true" manyToOneCertificateMappingsEnabled="false" defaultLogonDomain="" logonMethod="Interactive">
<oneToOneMappings>
<add enabled="true" userName="banana" password="[enc:IISCngProvider:aHdlxks+PoKuiv2SdlE7iFbgFasNITBv4gCBq2TmTXMeBM8hzQJVUQbvLobW+0FfsaEe/p4y5uIQiWmg6xnZIA==:enc]" certificate="banana" />
<add enabled="true" userName="2bananas" password="[enc:IISCngProvider:lbMChWQ1rxeVyFOBddSDtiJsGvSPmCeeVQ2HXZfmqApkAkSM2PVPK4YnUu4ENevVqPvtf/XqOp4hy2YWcM0SAudzc1aB8yrwzpwxkSeD9+4=:enc]" certificate="2bananas" />
</oneToOneMappings>
</iisClientCertificateMappingAuthentication>
<basicAuthentication enabled="false" />
<windowsAuthentication enabled="false" />
<anonymousAuthentication enabled="false" />
</authentication>
<access sslFlags="Ssl, SslNegotiateCert, SslRequireCert" />
</security>
</system.webServer>
</location>
</configuration>
【问题讨论】: