WCF wsdualhttpBinding自定义用户名

WCF wsdualhttpBinding自定义用户名,wcf,wsdualhttpbinding,Wcf,Wsdualhttpbinding,我犯了一个非常恼人的错误: 我的场景-使用wsdualhttpbinding在WCF中实现的简单消息/邮件服务器\客户端(双用于回调,新消息的在线更新) 所有安全配置都是用代码编写的(根本没有*.config)。在第一次连接时,客户端抛出以下命令 [System.Security.Cryptography.CryptographyException]={“错误长度。\r\n”} 由于内部异常为NULL,因此不可能钻得更深。 服务器配置: WSDualHttpBinding bindin

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

所有安全配置都是用代码编写的(根本没有*.config)。在第一次连接时,客户端抛出以下命令 [System.Security.Cryptography.CryptographyException]={“错误长度。\r\n”} 由于内部异常为NULL,因此不可能钻得更深。 服务器配置:

     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的新手,所以我可能缺少一些基本的概念

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


编辑以获取其他信息:

在我的例子中,我使用了
CustomBinding
而不是
WSDualHttpBinding
,但是我最终在双方(客户端和服务器)使用了完全相同的证书作为ServiceCertificate和ClientCertificate

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

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
security,所以它希望在两个方向上都有消息级别的安全性,所以它在两侧都需要一个证书(ServiceCertificate和ClientCertificate)


我只是说,我可能完全错了;尝试一下,看看这是否有助于…

实现相同的目标-即相同的证书位置?如果我有(并且将有)多个客户端通过intranet呢?在我的案例中,我确实使用了完全相同的证书。但我的情况可能略有不同,因为我使用了完整的
CustomBinding
而不是
WSDualHttpBinding
。我将在上面的回复中添加更多细节…非常感谢,我现在就试试。在我开始更改所有内容之前,此实现支持回调,对吗?@Stas:我实际上根本没有使用回调,所以我无法专门与他们交谈,抱歉。