Search code examples
c#wcfapp-configwcf-bindingservice-model

Get setting from C# App.config file


I have an app.config file. It's from a sample given to me for an API I have to use... I want to get a setting from the file so that I can use the settings from there and not have to duplicate efforts.

How can I get the words "FindMe", "LocalMachine" and "My" in this app.config file (to drive pulling a certificate from the given information)?

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>...</startup>
  <system.serviceModel>
    <bindings>...</bindings>
    <client>...</client>
    <behaviors>
      <endpointBehaviors>
        <behavior name="ClientCertificateBehavior">
          <clientCredentials>
            <clientCertificate findValue="FindMe" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName"/>
            <serviceCertificate><authentication certificateValidationMode="None"/></serviceCertificate>
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

I'm looking to see if I can find it in System.ServiceModel.Configuration or ConfigurationManager, but I'm not seeing how to get those specific values.

Edit:

I think I'm real close, but I can't seem to get the values.

enter image description here


Solution

  • Using Gandarez's comment and Phils answer as a launching board, I was able to poke my way into this solution. It's far from finished, but it'll allow me to get the values and I can fine tune it as needed:

    using System.Configuration;
    using System.ServiceModel.Configuration;
    using config = System.Configuration.Configuration;
    namespace Client
    {
        public class Program
        {
            private static void Main(string[] args)
            {
                config Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                ServiceModelSectionGroup Group = ServiceModelSectionGroup.GetSectionGroup(Config);
                BehaviorsSection Behaviors = Group.Behaviors;
                EndpointBehaviorElementCollection EndpointBehaviors = Behaviors.EndpointBehaviors;
                EndpointBehaviorElement EndpointBehavior = EndpointBehaviors[0];
                ClientCredentialsElement ClientCredential = (ClientCredentialsElement) EndpointBehavior[0];
                var ClientCertificate = ClientCredential.ClientCertificate;
    
                var findValue = ClientCertificate.FindValue;
                var storeName = ClientCertificate.StoreName;
                var storeLocation = ClientCertificate.StoreLocation;
                var X509FindType = ClientCertificate.X509FindType;
            }
        }
    }
    

    enter image description here