Get setting from C# App.config file

2019-08-05 09:30发布

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

2条回答
Bombasti
2楼-- · 2019-08-05 09:56

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

查看更多
SAY GOODBYE
3楼-- · 2019-08-05 09:57

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;
    }
查看更多
登录 后发表回答