Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF通道工厂与服务引用的身份验证差异_C#_Wcf - Fatal编程技术网

C# WCF通道工厂与服务引用的身份验证差异

C# WCF通道工厂与服务引用的身份验证差异,c#,wcf,C#,Wcf,我使用下面描述的两种方式在WCF服务中使用第三方https web服务 使用服务引用 ServiceClient client=new ServiceClient(); client.ClientCredentials.UserName.UserName ="xxx"; client.ClientCredentials.UserName.Password = "pwd"; ServicePointManager.ServerCertificateValidationCallback += (se

我使用下面描述的两种方式在WCF服务中使用第三方https web服务

  • 使用服务引用

    ServiceClient client=new ServiceClient();
    client.ClientCredentials.UserName.UserName ="xxx";
    client.ClientCredentials.UserName.Password = "pwd";
    ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain,                       sslPolicyErrors) => true;
    ServiceResponse response=client.GetData();
    
  • 2.使用渠道工厂

      ChannelFactory<IService> client = new ChannelFactory<IService>(binding, address);
      var proxy = client.CreateChannel();
      client.Credentials.UserName.UserName ="xxx";
      client.Credentials.UserName.Password ="pwd";
      ServiceResponse response=client.GetData();
    
    ChannelFactory客户端=新的ChannelFactory(绑定,地址);
    var proxy=client.CreateChannel();
    client.Credentials.UserName.UserName=“xxx”;
    client.Credentials.UserName.Password=“pwd”;
    ServiceResponse=client.GetData();
    

    我能够使用第一种方法传递安全凭据,并且能够从第三方web服务获得响应。但是,当我使用第二种方法时,我无法获得响应。我可以看到,用户名、密码是用第一种方法添加到即将发出的SOAP消息的安全头中的,而不是用第二种方法。如果有人能在这里提出一些关于channel factory方法的建议,我将非常高兴。

    问题是当您在当前代码中分配凭据时,您在创建工厂后创建代理,然后将凭据分配给工厂。但这对创建的通道没有影响:

    ChannelFactory<IService> client = new ChannelFactory<IService>(binding, address);
    var proxy = client.CreateChannel();
    
    client.Credentials.UserName.UserName ="xxx";
    client.Credentials.UserName.Password ="pwd";
    

    问题是在分配凭据时-在当前代码中,在创建工厂后创建代理,然后将凭据分配给工厂。但这对创建的通道没有影响:

    ChannelFactory<IService> client = new ChannelFactory<IService>(binding, address);
    var proxy = client.CreateChannel();
    
    client.Credentials.UserName.UserName ="xxx";
    client.Credentials.UserName.Password ="pwd";
    

    这两种方法之间的绑定可能存在差异?我对这两种方法都使用WSHttpBinding。您检查了绑定的安全参数了吗?是的,我检查了绑定的参数。这两种方法之间的绑定可能存在差异?我对这两种方法都使用WSHttpBinding。您检查了关于安全性的绑定?是的,我检查了绑定的参数。