1

我有一个 app.config 文件。它来自给我的一个我必须使用的 API 的示例......我想从文件中获取一个设置,以便我可以使用那里的设置而不必重复工作。

如何在此 app.config 文件中获取“FindMe”、“LocalMachine”和“My”字样(以驱动从给定信息中提取证书)?

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

我正在寻找是否可以在 System.ServiceModel.Configuration 或 ConfigurationManager 中找到它,但我没有看到如何获取这些特定值。

编辑:

我想我真的很接近,但我似乎无法获得价值观。

在此处输入图像描述

4

2 回答 2

2

使用 Gandarez 的评论和 Phils 的回答作为启动板,我能够进入这个解决方案。它远未完成,但它可以让我获得值,我可以根据需要对其进行微调:

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

在此处输入图像描述

于 2014-08-19T01:46:40.573 回答
1

一旦您可以访问ServiceModelSectionGroup,您就可以访问模型的各个部分。例如Behaviors.EndpointBehaviors收集

WCF 部分信息

 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;
    }
于 2014-08-18T21:03:57.697 回答