有没有办法从 IIS 中删除单个 OneToOneMapping?
Posted
技术标签:
【中文标题】有没有办法从 IIS 中删除单个 OneToOneMapping?【英文标题】:Is there a way to delete a single OneToOneMapping from IIS? 【发布时间】: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>
【问题讨论】:
【参考方案1】:我可以使用此代码将香蕉和 2banana 添加到 OneToOneMappings。
internal static class Sample
private static void Main()
using(ServerManager serverManager = new ServerManager())
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection iisClientCertificateMappingAuthenticationSection = config.GetSection("system.webServer/security/authentication/iisClientCertificateMappingAuthentication", "default");
iisClientCertificateMappingAuthenticationSection["enabled"] = true;
iisClientCertificateMappingAuthenticationSection["manyToOneCertificateMappingsEnabled"] = false;
iisClientCertificateMappingAuthenticationSection["logonMethod"] = @"Interactive";
ConfigurationElementCollection oneToOneMappingsCollection = iisClientCertificateMappingAuthenticationSection.GetCollection("oneToOneMappings");
ConfigurationElement addElement = oneToOneMappingsCollection.CreateElement("add");
addElement["userName"] = @"banana";
addElement["password"] = @"banana";
addElement["certificate"] = @"banana";
oneToOneMappingsCollection.Add(addElement);
ConfigurationElement addElement1 = oneToOneMappingsCollection.CreateElement("add");
addElement1["userName"] = @"2bananas";
addElement1["password"] = @"2bananas";
addElement1["certificate"] = @"2bananas";
oneToOneMappingsCollection.Add(addElement1);
serverManager.CommitChanges();
当我要删除香蕉时,代码是:
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 iisClientCertificateMappingAuthenticationSection = config.GetSection("system.webServer/security/authentication/iisClientCertificateMappingAuthentication", "default");
ConfigurationElementCollection oneToOneMappingsCollection = iisClientCertificateMappingAuthenticationSection.GetCollection("oneToOneMappings");
ConfigurationElement addElement = FindElement(oneToOneMappingsCollection, "add", "certificate", @"banana");
if (addElement == null) throw new InvalidOperationException("Element not found!");
oneToOneMappingsCollection.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;
当我想删除它们时,代码是:
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 iisClientCertificateMappingAuthenticationSection = config.GetSection("system.webServer/security/authentication/iisClientCertificateMappingAuthentication", "default");
ConfigurationElementCollection oneToOneMappingsCollection = iisClientCertificateMappingAuthenticationSection.GetCollection("oneToOneMappings");
oneToOneMappingsCollection.Clear();
serverManager.CommitChanges();
配置如下:
【讨论】:
以上是关于有没有办法从 IIS 中删除单个 OneToOneMapping?的主要内容,如果未能解决你的问题,请参考以下文章
javax.persistence.OneToOne.orphanRemoval()Z。要详细的解决办法。不要说是EJB的事情了,完全不好使。