如何避免在客户端设置 DnsEndpointIdentity
How to avoid setting DnsEndpointIdentity on client side
基本上我有一个应用程序,其中有一个客户端和两个通过 netTcpBinding 或 netNamedPipe 公开端点的服务。此外,服务和客户端之间的整个通信都通过证书进行保护(目前我使用的是自信任证书)。
工作正常,但在创建 EndpointAddress instance, I need also set DnsEndpointIdentity 期间使用服务端使用的证书名称。所以它看起来像这样:
new EndpointAddress(serviceUrl, new DnsEndpointIdentity("MyCertificateName"));
所以我的问题是:
这是正常的吗?有没有办法避免在客户端设置它?
这是正常现象,身份不能无视,代表服务器的身份。第三方可以冒充服务程序让客户端调用,以达到窃取客户端信息的目的。为保证客户端不会找错服务器,证书中的hostname(subject)必须与客户端提供的hostname(DNS identity)一致。我们也可以使用证书的 public 密钥作为身份,因为证书的 public 密钥对外是 public。
<identity>
<rsa value="...."/>
</identity>
这是获取证书的 public 密钥的示例代码。
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();
顺便说一下,这些配置可以在使用“添加服务引用”对话框调用服务时自动生成。
如果有什么我可以帮忙的,请随时告诉我。
基本上我有一个应用程序,其中有一个客户端和两个通过 netTcpBinding 或 netNamedPipe 公开端点的服务。此外,服务和客户端之间的整个通信都通过证书进行保护(目前我使用的是自信任证书)。
工作正常,但在创建 EndpointAddress instance, I need also set DnsEndpointIdentity 期间使用服务端使用的证书名称。所以它看起来像这样:
new EndpointAddress(serviceUrl, new DnsEndpointIdentity("MyCertificateName"));
所以我的问题是: 这是正常的吗?有没有办法避免在客户端设置它?
这是正常现象,身份不能无视,代表服务器的身份。第三方可以冒充服务程序让客户端调用,以达到窃取客户端信息的目的。为保证客户端不会找错服务器,证书中的hostname(subject)必须与客户端提供的hostname(DNS identity)一致。我们也可以使用证书的 public 密钥作为身份,因为证书的 public 密钥对外是 public。
<identity>
<rsa value="...."/>
</identity>
这是获取证书的 public 密钥的示例代码。
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();
顺便说一下,这些配置可以在使用“添加服务引用”对话框调用服务时自动生成。
如果有什么我可以帮忙的,请随时告诉我。