Home > Net >  Is there a way to delete a single OneToOneMapping from IIS?
Is there a way to delete a single OneToOneMapping from IIS?

Time:12-15

Using the following code I was able to add a user to my OneToOneMappings section in my IIS config, but how do I got about removing a user again?

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();
      }
   }
}

I have tried printing the ConfigurationElementCollection to the console to have a look at that, but it doesn't show me any information (I might just be too dumb to print it properly). What I want is to find a single user, and then delete that 'add' element from the config, which currently looks something like this:

<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>

CodePudding user response:

I can use this code to add banana and 2banana to 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();
    }
  }
}

When I want to remove banana, the code is:

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;
}
}

When I want to remove both of them, the code is:

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();
    }
}
}

Configuration look like this: enter image description here

  • Related