【问题标题】:Why does service allow clients with wrong (but trusted) certificates?为什么服务允许使用错误(但受信任)证书的客户端?
【发布时间】:2015-05-01 10:36:12
【问题描述】:

我将预期的客户端证书设置为“A”:

        host.Credentials.ClientCertificate.SetCertificate("A", ...);
        host.Credentials.ServiceCertificate.SetCertificate("B", ...);

绑定:

new NetTcpBinding
                    {
                        Security =
                        {
                            Mode = SecurityMode.TransportWithMessageCredential,
                            Transport = { ProtectionLevel = ProtectionLevel.EncryptAndSign },
                            Message = { ClientCredentialType = MessageCredentialType.Certificate }
                        }
                    }

我希望服务器只允许具有证书“A”的客户端。但相反,它也允许其他受信任的证书。我已将客户端 app.config 更改为使用“B”而不是“A”,它仍然有效!

我的设置有什么问题?

【问题讨论】:

  • 正如您在我的回答中看到的那样,此方法不适用于选择可接受的客户端证书。

标签: c# wcf wcf-binding wcf-security


【解决方案1】:

host.Credentials.ClientCertificate.SetCertificate("A", ...);

并不意味着只有拥有证书A的客户端才能连接。

如果您只想允许某些类型的证书,则需要检查服务器端的 CertificateValidator。

看看: https://msdn.microsoft.com/en-us/library/aa354512%28v=vs.110%29.aspx

如果您还有更多问题,请随时问我

编辑:

public class CustomX509CertificateValidator : X509CertificateValidator
{
  public override void Validate ( X509Certificate2 certificate )
  {
   // Only accept self-issued certificates for example
   if (certificate.Subject != certificate.Issuer)
     throw new Exception("Certificate is not self-issued");
   }
}

然后:

serviceHost.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.Custom;
serviceHost.Credentials.ClientCertificate.Authentication.CustomCertificateValidator = new CustomX509CertificateValidator();

【讨论】:

  • 我会阅读它,但您能否发布任何示例,说明如何使用 CertificateValidator 仅指定应允许的确切客户端证书?
  • 在我的编辑帖子中,主机上的验证器将只接受颁发者为主题的证书,即所谓的自签名。
  • 我可以使用 certificate.Equals 来检查它是否正确,不是吗?
  • 您可以使用certificate.equals来检查两个证书是否相同
猜你喜欢
  • 1970-01-01
  • 2010-09-13
  • 1970-01-01
  • 2021-10-03
  • 2012-10-11
  • 1970-01-01
  • 2014-12-02
  • 1970-01-01
  • 2012-02-26
相关资源
最近更新 更多