代码之家  ›  专栏  ›  技术社区  ›  CodingWithSpike

自定义凭据上未设置WCF证书

  •  2
  • CodingWithSpike  · 技术社区  · 17 年前

    我在WCF中有一个ClientCredentials的自定义实现。ClientCredentials的两个基本属性是ClientCertificate和ServiceCertificate,如图所示 here (MSDN) .

            <endpointBehaviors>
                <behavior name="MyCustomEndpointBehavior">
                    <clientCredentials type="MyApp.Security.CentralAuthClientCredentials, MyApp">
                        <clientCertificate findValue="localhost" x509FindType="FindBySubjectName" storeLocation="CurrentUser" storeName="My" />
                        <serviceCertificate>
                            <defaultCertificate findValue="localhost" x509FindType="FindBySubjectName" storeLocation="CurrentUser" storeName="My" />
                            <authentication certificateValidationMode="None" revocationMode="NoCheck" />
                        </serviceCertificate>
                    </clientCredentials>
                </behavior>
            </endpointBehaviors>
    

    使用用户名身份验证。它使用证书对消息的用户名和密码进行加密。然后我更改为自定义ClientCredentials。

    在运行时,以下是在我的CentralAuthClientCredentials上设置的属性:

    this.ClientCertificate = System.ServiceModel.Security.X509CertificateInitiatorClientCredential
    this.ClientCertificate.Certificate = null
    this.ServiceCertificate = System.ServiceModel.Security.X509CertificateRecipientClientCredential
    this.ServiceCertificate.DefaultCertificate = null
    

    我可以在凭据的构造函数中使用一些代码来手动从配置中获取这些证书吗?


    更新

    思考

    this.ChannelFactory.Endpoint.Behaviors.Remove<ClientCredentials>();
    this.ChannelFactory.Endpoint.Behaviors.Add(new CentralAuthClientCredentials());
    return base.Channel.MyServiceMethod();
    

    删除旧凭据并添加我自己的自定义凭据。然而,这正在创建一个没有设置证书的新实例!哎呀!

    1 回复  |  直到 17 年前
        1
  •  1
  •   CodingWithSpike    17 年前

    为了将此标记为已回答,我添加了自己的解决方案,即从生成新凭据的客户端中删除此代码:

    this.ChannelFactory.Endpoint.Behaviors.Remove<ClientCredentials>();
    this.ChannelFactory.Endpoint.Behaviors.Add(new CentralAuthClientCredentials());