【问题标题】:Is there a way to delete a single OneToOneMapping from IIS?有没有办法从 IIS 中删除单个 OneToOneMapping?
【发布时间】: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>

【问题讨论】:

    标签: c# asp.net iis


    【解决方案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();
        }
    }
    }
    

    配置如下:

    【讨论】:

      猜你喜欢
      • 2011-03-24
      • 2019-07-08
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 2020-06-11
      • 2022-11-12
      • 1970-01-01
      • 2017-03-18
      相关资源
      最近更新 更多