要在本地使用证书测试WCF服务,您可以按照以下步骤进行操作:
创建自签名证书:
makecert -r -pe -n "CN=MyWCFServiceCert" -ss my -sr localmachine -sky exchange -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12
配置WCF服务以使用证书:
创建用于测试的客户端应用程序:
Install-Package System.ServiceModel -Version 4.7.0
Install-Package System.Runtime.Serialization -Version 4.3.0
using System;
using System.ServiceModel;
namespace MyWCFClient
{
class Program
{
static void Main(string[] args)
{
// 设置证书验证回调
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
// 创建WCF客户端
var binding = new BasicHttpBinding();
var endpointAddress = new EndpointAddress("https://localhost/MyWCFService/MyService.svc");
var client = new MyServiceClient(binding, endpointAddress);
// 调用服务方法
var result = client.MyServiceMethod();
// 处理服务响应
Console.WriteLine(result);
// 关闭客户端
client.Close();
Console.ReadLine();
}
}
}
运行测试:
请注意,由于使用了自签名证书,客户端在与服务建立安全连接时不会进行证书验证。在实际生产环境中,您应该使用由受信任的证书颁发机构签名的证书。
下一篇:本地守护程序的SSH指纹