1

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

6
  • 2
    stackoverflow.com/questions/14032875/… Commented Aug 18, 2014 at 20:24
  • <appSettings> <add key="MachineName" value="LocalMachine" /> </appSettings> Commented Aug 18, 2014 at 20:31
  • @dashsa I could do that, if i simply wanted the settings in a single file... that still adds two locations with the same setting. The goal is to be able to pull the information I want from one spot so I don't have to upkeep two spots (even if in the same file). Duplicate settings = forgetting to update the second one. Commented Aug 18, 2014 at 20:35
  • 1
    @Gandarez Playing around, I've got a behaviors element var config = ...; var group = ...; var behaviors = group.Behaviors.EndpointBehaviors;, which seems to be a good starting point. Trying to find the attributes for the clientCredentials still, but I feel that answer has gotten me a little closer. Commented Aug 18, 2014 at 21:00
  • This gets me close, and I'm still looking... but that answer doesn't quite get me there. I've got the SectionGroups, but I can't seem to nail down the individual properties/attributes. I have a feeling that it's right under my nose. Commented Aug 19, 2014 at 0:26

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

Once you have access to the ServiceModelSectionGroup, you can access the various parts of the model. eg Behaviors.EndpointBehaviors collection

WCF section info

 public ServiceModelSectionGroup GetServiceModelSectionGroup() {
        var cfg = GetConfig();

        ServiceModelSectionGroup serviceModelSection = ServiceModelSectionGroup.GetSectionGroup(cfg);

        return serviceModelSection;
    }


public Configuration GetConfig() {
        if (_cfg == null) {
            if (HostingEnvironment.IsHosted) // running inside asp.net ?
            { //yes so read web.config at hosting virtual path level
                _cfg = WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath);
            }
            else { //no, se we are testing or running exe version admin tool for example, look for an APP.CONFIG file
                //var x = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                _cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            }
        }
        return _cfg;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.