简体   繁体   English

WCF Channel Factory身份验证与服务参考的区别

[英]Difference in authentication of WCF Channel Factory vs Service Reference

I am consuming a 3rd party https web service in a WCF service using the 2 ways described below. 我正在使用以下所述的2种方式在WCF服务中使用第三方的https Web服务。

  1. Using Service Reference 使用服务参考

     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.Using channel factory 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();

I am able to pass the security credentials using the first approach and i am able to get the respone back from the 3rd party web service.But I am unable to get the response when i use the second approach. 我可以使用第一种方法传递安全证书,并且可以从第三方Web服务获取响应。但是,当我使用第二种方法时,我无法获得响应。 I can see that the username,password are added in the security header of the outoing SOAP message with the first approach but not with the second approach.I would be glad if some one can throw some suggestion here about the channel factory approach. 我可以看到用户名,密码是使用第一种方法添加到远程SOAP消息的安全标头中的,而不是使用第二种方法。如果有人可以在此处提出有关通道工厂方法的建议,我将感到非常高兴。

The issue is when you're assigning the credentials - in your current code, you're creating the proxy after you create the factory, and then you assign the credentials to the factory. 问题是当您分配凭据时-在当前代码中,您是在创建工厂之后创建代理的, 然后将凭据分配给工厂。 But that has no effect on the created channel: 但这对创建的通道没有影响:

ChannelFactory<IService> client = new ChannelFactory<IService>(binding, address);
var proxy = client.CreateChannel();

client.Credentials.UserName.UserName ="xxx";
client.Credentials.UserName.Password ="pwd";

var proxy is a an implementation of IChannel - setting the credentials on the factory ( client ) has no effect on the already created channels - just the ones created later. var proxyIChannel的一种实现-在工厂( client )上设置凭据不会影响已经创建的通道,而只会影响以后创建的通道。

Try setting the credentials and then calling CreateChannel() : 尝试设置凭据, 然后调用CreateChannel()

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

var proxy = client.CreateChannel();
ServiceResponse response=client.GetData();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM