【发布时间】:2018-05-24 04:48:37
【问题描述】:
首先,感谢您的时间和关注!
我有一个需要 x509 证书进行身份验证的 WCF Web 服务。此 Web 服务已添加为连接服务和在 .Net Standard 2.0 库中生成的参考类。我想使用这个 WCF Web 服务。但是,我遇到的是这个;我试图分配给 BasicHttpsBinding 的 Security 属性的 BasicHttpsSecurity 对象似乎没有任何可访问的构造函数...
在这种情况下,如何将 ClientCredentialType 属性设置为 Certificate?
在我制作的完整 .Net Framework 测试项目中,我的代码与预期完全相同。看来绑定和 x509 证书是根据需要设置的。我只是无法设置 Security 属性来指定我希望它实际使用证书...:/
这是我用来设置客户端的代码。它似乎在完整的 .Net Framework 测试应用程序中运行良好。主要问题是“Security = new BasicHttpsSecurity()”告诉我没有接受 0 个参数的构造函数......但是,我根本没有看到任何构造函数。因此,我无法告诉绑定我想使用 HttpClientCredentialType.Certificate 作为我的 ClientCredentialType。
private TestServiceClient GetTestServiceClient()
{
var address = new EndpointAddress("https://known.to.be/working/service/endpoint");
var binding = new BasicHttpsBinding()
{
Name = "basic_SSL_Cert",
// ... other properties that work fine in full .Net Framework test.
// I can't instantiate/initialize this...
Security = new BasicHttpsSecurity()
{
Mode = BasicHttpsSecurityMode.Transport,
Transport = new HttpTransportSecurity()
{
// ... so I can't set this :(
ClientCredentialType = HttpClientCredentialType.Certificate,
ProxyCredentialType = HttpProxyCredentialType.None
}
}
};
var client = new TestServiceCLient(binding, address);
client.ClientCredentials.ClientCertificate.SetCertificate(
StoreLocation.LocalMachine,
StoreName.My,
X509FindType.FindBySubjectDistinguishedName,
"I can find the cert no problem."
);
return client;
}
非常感谢任何有关如何使用需要来自 .Net Standard 2.0 库的 x509 证书的 WCF Web 服务的建议。如果我说这一切都错了,请直接引导我。
提前感谢您的宝贵时间!
【问题讨论】:
-
启用 SSL 安全可以在配置文件中完成
-
@Suhail 感谢您的评论。我有一个 appsettings.json 配置文件,而不是传统的 app/web.config。将从 ASPNet Core Web API 中调用此 WCF Web 服务。我打算从 appsettings.json 中提取应用程序设置并在代码中填充绑定。有没有更好的办法?
标签: c# .net wcf authentication ssl