自定义 X509 证书验证程序检查请求程序与 CN



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

需要根据提供证书的域检查证书上的 CN 名称,但我不知道我有权从 X509CertificateValidator 中访问请求。

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

> 我还没有找到任何方法可以从 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");
        }
    }

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

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

相关内容

最新更新