0

我遇到了一个非常烦人的错误:我的场景 - 使用 wsdualhttpbinding 在 WCF 中实现的简单消息/邮件服务器\客户端(双用于回调,新消息的在线更新)。

所有安全配置都是用代码编写的(根本没有 *.config)。在第一次连接时,客户端会抛出以下 [System.Security.Cryptography.CryptographicException] = {"Bad Length.\r\n"} with NULL inner exception ,因此无法进行更深入的研究。服务器配置:

     WSDualHttpBinding binding = new   WSDualHttpBinding(WSDualHttpSecurityMode.Message);
    binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;

        Uri baseServiceAddress = new Uri(@"http://"+Environment.MachineName+":7921/Mail/");                 
        host = new ServiceHost(theMightyMailServer,baseServiceAddress);             

        host.Credentials.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
        host.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
        host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = validator;
        host.Credentials.ServiceCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.Root, X509FindType.FindByIssuerName, "MailServer");
        ServiceDebugBehavior d = new ServiceDebugBehavior();
        d.IncludeExceptionDetailInFaults = true;
        host.Description.Behaviors.Remove<ServiceDebugBehavior>();
        host.Description.Behaviors.Add(d);
        ServiceMetadataBehavior b = new ServiceMetadataBehavior();
        b.HttpGetEnabled = true;
        host.Description.Behaviors.Remove<ServiceMetadataBehavior>();
        host.Description.Behaviors.Add(b);
        var mexBinding = MetadataExchangeBindings.CreateMexHttpBinding();
        host.AddServiceEndpoint(typeof(IMailServer), binding, "Service");
        host.AddServiceEndpoint(typeof(IMetadataExchange),mexBinding,"");


        host.Open();

客户端配置:

           client = new MailServerReference.MailServerClient(new InstanceContext(this));

            client.ClientCredentials.UserName.UserName = currentUser.UserName;
            client.ClientCredentials.UserName.Password = currentUser.Password;
                client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
            client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.Root,X509FindType.FindByIssuerName, "MailServer");
            currentUser.ID = client.getUID();
            client.RegisterOnServer(currentUser.ID);
            return true;

        }
        catch (Exception ex) { MessageBox.Show(ex.Message); return false; }

任何帮助将不胜感激。顺便说一句,我是 WCF 的新手,所以也许我缺少一些基本概念。

4

1 回答 1

0

我会尝试在客户端和服务器上将ServiceCertificateand设置ClientCertificate为相同的东西。至少,我就是这样做的。已经有一段时间了,但我认为当只设置一个证书或另一个证书时我遇到了问题,而不是两者。


编辑以获取更多信息:

在我的情况下,我使用 aCustomBinding而不是 aWSDualHttpBinding但我最终在双方(客户端和服务器)上使用完全相同的证书用于 ServiceCertificate 和 ClientCertificate。

更具体地说,我有一个扩展类System.ServiceModel.Description.ServiceCredentials

public class MyServiceCredentials : ServiceCredentials
{
    public MyServiceCredentials() : base()
    {
        LoadCertificate();
    }

    public MyServiceCredentials(MyServiceCredentials other) : base(other)
    {
        LoadCertificate();
    }

    private void LoadCertificate()
    {
        ServiceCertificate.Certificate = _cert;
        ClientCertificate.Certificate = _cert;
        ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
        ClientCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck;
   }
}

我将其中一个传递给客户端和服务器实现中的 CustomBinding 构造函数。

无论如何,我认为因为Message为绑定打开了安全性,它希望在两个方向上都有消息级别的安全性,所以它需要双方都有一个证书(ServiceCertificate 和 ClientCertificate)。

不过,我可能完全错了,我只是说;试试看是否有帮助...

于 2011-05-05T19:15:17.610 回答