如何将此 app.config WCF 客户端配置转换为代码?

2023-12-29

我已获得以下 app.config 条目,但是我希望将其作为代码添加到我的应用程序中,以便更好地理解 WCF。

有没有转换器,或者有人可以提供代码。谢谢。

 <system.serviceModel>
    <client>
      <endpoint name="QA" address="https://subdomain1.theirdomain.com/5067/Sample1"
      behaviorConfiguration="WSSecBehavior" binding="customBinding"
      bindingConfiguration="Soap11_Secure"
      contract="star.starTransportPortTypes" />
      <endpoint name="PROD" address="https://subdomain1.theirdomain.com/5067/Sample1"
      behaviorConfiguration="WSSecBehavior" binding="customBinding"
      bindingConfiguration="Soap11_Secure"
      contract="star.starTransportPortTypes" />
    </client>
    <bindings>
      <customBinding>
        <binding name="Soap11_Secure">
          <textMessageEncoding messageVersion="Soap11" />
          <security defaultAlgorithmSuite="Basic128Rsa15"
          allowSerializedSigningTokenOnReply="true"
          authenticationMode="MutualCertificate"
          messageProtectionOrder="SignBeforeEncrypt"
          messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10">
          </security>
          <httpsTransport maxBufferSize="5000000"
          maxReceivedMessageSize="5000000" />
        </binding>
      </customBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WSSecBehavior">
          <clientCredentials>
            <clientCertificate storeLocation="CurrentUser"
            storeName="My" x509FindType="FindBySubjectName"
            findValue="*.mydomain.org.uk" />
            <serviceCertificate>
              <!-- you my have to add this if your client cannot check revocations -->
              <authentication revocationMode="NoCheck" />
              <scopedCertificates>
                <add targetUri="https://subdomain1.theirdomain.com/Sample1"
                storeName="AddressBook"
                x509FindType="FindBySubjectName"
                findValue="subdomain1.theirdomain.com" />
              </scopedCertificates>
            </serviceCertificate>
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

到目前为止我有这个:

    Dim asbe As New Channels.AsymmetricSecurityBindingElement

    asbe.MessageSecurityVersion = MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10
    asbe.DefaultAlgorithmSuite = Security.SecurityAlgorithmSuite.Basic128Rsa15  'By default, AES-128 is used as the encryption algorithm.
    asbe.AllowSerializedSigningTokenOnReply = True
    asbe.InitiatorTokenParameters = New ServiceModel.Security.Tokens.X509SecurityTokenParameters
    asbe.RecipientTokenParameters = New ServiceModel.Security.Tokens.X509SecurityTokenParameters
    asbe.MessageProtectionOrder = Security.MessageProtectionOrder.SignBeforeEncrypt

    'Add the elements to the custom binding
    Dim myBinding As New CustomBinding

    'element order is important - see http://msdn.microsoft.com/en-us/library/ms733893(v=vs.90).aspx

    'Protocol Binding Elements (security)
    myBinding.Elements.Add(asbe)

    'Encoding Binding Element  
    myBinding.Elements.Add(New TextMessageEncodingBindingElement(MessageVersion.Soap11, System.Text.Encoding.UTF8))

    'Transport Binding Element
    Dim httpsBindingElement As New HttpsTransportBindingElement()
    httpsBindingElement.MaxBufferSize = 5000000
    httpsBindingElement.MaxReceivedMessageSize = 5000000

    myBinding.Elements.Add(httpsBindingElement)

    Dim epi As EndpointIdentity = EndpointIdentity.CreateDnsIdentity("subdomain.theirdomain.com")

    Dim epuri As Uri = New Uri("https://subsomain1.theirdomain.com/5067/ProcessRepairOrder")
    Dim ea As New EndpointAddress(epuri, epi, New AddressHeaderCollection)

    ' Create the client. 
    Dim starClientProxy As New wcfStarServiceProxy.starTransportPortTypesClient(myBinding, ea)

    ' Specify a certificate to use for authenticating the client.

    starClientProxy.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "*.mydomain.org.uk")
    starClientProxy.ClientCredentials.ServiceCertificate.SetDefaultCertificate(StoreLocation.CurrentUser, StoreName.AddressBook, X509FindType.FindBySubjectName, "subdomain.theirdomain.com")

    ' Begin using the client.

    Dim response As wcfStarServiceProxy.AcknowledgeRepairOrderPayload = starClientProxy.ProcessMessage(payload)

我不确定我还需要做什么。我找不到如何在 AmetrySecurityBindingElement 上设置authenticationMode =“MutualCertificate”。

另外,是否有任何工具可以在代码和配置之间进行转换?


不要直接创建 AsymmetrySecurityBindingElement。而是使用这样的东西:

SecurityBindingElement.CreateMutualCertificateBindingElement
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将此 app.config WCF 客户端配置转换为代码? 的相关文章

随机推荐