如何避免在客户端设置DnsEndpointIdentity

可怜的开发者

因此,基本上,我有一个应用程序,其中有一个客户端和两个通过netTcpBinding或netNamedPipe公开终结点的服务。此外,服务和客户端之间的整个通信都通过证书进行保护(目前,我使用的是自信任证书)。

它工作正常,但是在创建EndpointAddress实例的过程中,我还需要将DnsEndpointIdentity设置为服务端使用的证书名称。所以看起来像这样:

new EndpointAddress(serviceUrl, new DnsEndpointIdentity("MyCertificateName"));

所以我的问题是:这正常吗?有没有办法避免在客户端设置它?

Abraham Qian

这是一种正常现象,身份不能无知,它表示服务器的身份。第三方可以模拟服务程序让客户端调用,以达到窃取客户端信息的目的。为确保客户端找不到错误的服务器,证书(主题)中的主机名必须与客户端提供的主机名(DNS身份)相同。我们也可以使用证书的公共密钥作为标识,因为证书的公共密钥是对外公开的。

<identity>
                  <rsa value="...."/>
                </identity>

这是获取证书公钥的示例代码。

X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2 cert = null;
            foreach (var certificate in store.Certificates)
            {
                if (certificate.Subject.Contains("myhostname"))
                {
                    cert = certificate;
                    break;
                }
            }
            if (cert==null)
            {
                return;
            }
            //output the public key
            string xmlKey = cert.PublicKey.Key.ToXmlString(false);
            //encoded
            string encodedkey = System.Net.WebUtility.HtmlEncode(xmlKey);
            Console.WriteLine($"Public key:\n{encodedkey}");
            store.Close();
            store.Dispose();

顺便说一下,使用“添加服务引用”对话框调用服务时,可以自动生成这些配置。
请随时告诉我是否有什么我可以帮助的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章