自定义X509CertificateValidator检查请求者对CN

时间:2012-12-13 16:14:23

标签: wcf wcf-security x509certificate

我有一个自定义X509CertificateValidator,它目前针对为WCF SOAP消息提供的证书验证一系列规则。

需要根据证书所在的域检查证书上的CN名称,但我不知道我可以从X509CertificateValidator中访问该请求。

有没有办法检查证书是否与请求域匹配?

1 个答案:

答案 0 :(得分:2)

我从X509CertificateValidator中找不到任何方法可以做到这一点,但是在服务范围内是可行的。

这是我的第一次剪裁 - 我将精炼它以使其更优雅,但这有效。

    private static void ValidateRequestIsFromCertificateDomain()
    {
        RemoteEndpointMessageProperty endpointProperty = OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
        var claimSet = OperationContext.Current.ServiceSecurityContext.AuthorizationContext.ClaimSets[0] as X509CertificateClaimSet;

        string domain = claimSet.X509Certificate.GetNameInfo(X509NameType.DnsName, false);
        var resolvedAddress = System.Net.Dns.GetHostAddresses(domain);

        if (resolvedAddress.Count() == 0 || endpointProperty.Address != resolvedAddress[0].ToString())
        {
            throw new SecurityException("Client address mismatch");
        }
    }

这并不是真正需要的,因为客户端使用只能使用其公钥解密的私钥来加密数据 - 因此您知道证书是由真实客户端呈现的。

但是,如果您像我一样将此作为集成要求,那么这可能对您有用。