在用C#编写的WCF客户端中验证证书

时间:2015-07-31 06:20:31

标签: c# wcf authentication ssl client

我有非常简单的测试客户端WCF服务。 WCF服务受用户和密码以及ssl保护。

我的测试客户端看起来像这样:

       ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(
            delegate
            {
                return true;
            });

        var binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
        var endpointAddress = new EndpointAddress(@"https://localhost:8083/WS/Service.svc");

        using (var service = new ServiceClient(binding, endpointAddress))
        {
            service.ClientCredentials.UserName.UserName = @"user";
            service.ClientCredentials.UserName.Password = @"password";

            var data = service.GetData();
        }

是否可以删除行:

       ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(
            delegate
            {
                return true;
            });

每当我删除此行时,我得到 SecurityNegotiationException

1 个答案:

答案 0 :(得分:0)

您似乎忽略了故意提供给您的所有有用信息。您使用的回调应该为您提供所需的所有信息:

ServicePointManager.ServerCertificateValidationCallback = 
        delegate(
        Object obj,
        X509Certificate certificate,
        X509Chain chain, 
        SslPolicyErrors errors)
        {
            // instead of the shortcut "true" here, evaluate the parameters!
            return true;
        };

确保找到errors的内容并在系统中修复它们。