Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.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上使用HttpTransportBindingElement设置代理凭据?_C#_Wcf_Wcf Security - Fatal编程技术网

C# 如何在WCF上使用HttpTransportBindingElement设置代理凭据?

C# 如何在WCF上使用HttpTransportBindingElement设置代理凭据?,c#,wcf,wcf-security,C#,Wcf,Wcf Security,我在端口80上使用HttpTransportBindingElement和IIS编码了一个WCF服务。 只要不使用代理,代码就可以正常工作。但是,如果客户有http代理,则WCF客户端和服务器之间的通信在这种情况下不起作用,原因是发生以下错误: “没有端点在侦听。。。这可以接受这个信息。这通常是由不正确的地址或SOAP操作引起的。” 必须仅通过代码使用设置 以下是我针对该问题的代码方法,但我坚持: bool SendClientRequest(Action<ICustomerService

我在端口80上使用HttpTransportBindingElement和IIS编码了一个WCF服务。 只要不使用代理,代码就可以正常工作。但是,如果客户有http代理,则WCF客户端和服务器之间的通信在这种情况下不起作用,原因是发生以下错误:

“没有端点在侦听。。。这可以接受这个信息。这通常是由不正确的地址或SOAP操作引起的。”

必须仅通过代码使用设置

以下是我针对该问题的代码方法,但我坚持:

bool SendClientRequest(Action<ICustomerService> channel)
{

  string proxy ="my.proxy.domain:8080";
  string user = "user1";
  string password="secret";

  // maybe i do not need this 3 lines!
  WebProxy webproxy = new WebProxy(proxy, true);
  webproxy.Credentials = new NetworkCredential(user, password);
  WebRequest.DefaultWebProxy = webproxy;

  CustomBinding customBinding = new CustomBinding();
  customBinding.Elements.Add(new HttpTransportBindingElement()
  {

   AuthenticationSchemes.None : AuthenticationSchemes.Basic,                
   ProxyAddress = string.IsNullOrEmpty(proxy) ? null : new Uri(proxy),
   UseDefaultWebProxy = false,
   BypassProxyOnLocal = true,
   TransferMode = TransferMode.Streamed,
   MaxReceivedMessageSize = 84087406592,
   MaxBufferPoolSize = 0x1000000,
   MaxBufferSize = 0x1000000

  });


  using (ChannelFactory<ICustomerService> factory = new  
  ChannelFactory<ICustomerService>(customBinding ))
  {
   IClientChannel contextChannel = null;
   string url = "http://my.domain.de/Distribution/eService.svc", 
   EndpointAddress ep = new EndpointAddress(url);
   ICustomerService clientChannel = factory.CreateChannel(ep);                


   contextChannel = clientChannel as IClientChannel;
   contextChannel.OperationTimeout = TimeSpan.FromMinutes(rcvTimeout );

   channel(clientChannel); // <- here i get the exception!
   return true;
  }
}
bool sendclienterrequest(动作频道)
{
string proxy=“my.proxy.domain:8080”;
字符串user=“user1”;
字符串password=“secret”;
//也许我不需要这三行!
WebProxy WebProxy=新的WebProxy(proxy,true);
webproxy.Credentials=新的网络凭据(用户、密码);
WebRequest.DefaultWebProxy=webproxy;
CustomBinding CustomBinding=新的CustomBinding();
添加(新的HttpTransportBindingElement()
{
AuthenticationSchemes.None:AuthenticationSchemes.Basic,
ProxyAddress=string.IsNullOrEmpty(代理)?null:新Uri(代理),
UseDefaultWebProxy=false,
BypassProxyOnLocal=true,
TransferMode=TransferMode.Streamed,
MaxReceivedMessageSize=84087406592,
MaxBufferPoolSize=0x1000000,
MaxBufferSize=0x1000000
});
使用(ChannelFactory工厂=新
ChannelFactory(定制绑定))
{
IClientChannel contextChannel=null;
字符串url=”http://my.domain.de/Distribution/eService.svc", 
EndpointAddress ep=新的EndpointAddress(url);
ICCustomerService clientChannel=factory.CreateChannel(ep);
contextChannel=clientChannel作为IClientChannel;
contextChannel.OperationTimeout=TimeSpan.FromMinutes(rcvTimeout);

频道(clientChannel);//我认为您有一些选择,下面我将详细介绍其中一些

首先,您可以将
UseDefaultWebProxy
设置为true。这意味着将自动从系统代理设置中检索代理信息,可在Internet Explorer(Internet选项>连接>LAN设置>代理服务器)中配置。如果您不需要指定代理使用的凭据,这可能是合适的

另一种对我有效的方法是在
HttpTransportBindingElement()中使用
ProxyAuthenticationScheme
属性
对象。此属性仅在
CustomBinding
类上可用,并允许指定用于对代理进行身份验证的身份验证方案。与此相结合,必须根据属性
ProxyAddress
设置代理服务器。最后但并非最不重要的是,针对prox使用的凭据y应该根据使用的身份验证方案设置,因此,例如,使用
AuthenticationSchemes.Ntlm
意味着在
ChannelFactory.ClientCredentials.Windows.ClientCredential
ChannelFactory.ClientCredentials.HttpDigest.ClientCredential

对于第二种方法,请务必注意在ChannelFactory中保留用于远程服务的凭据与用于代理服务器的凭据之间的区别。为了清晰起见,我在下面的代码示例中突出显示了这些凭据:

// Example service call using a CustomBinding that is configured for client
// authentication based on a user name and password sent as part of the message.
var binding = new CustomBinding();

TransportSecurityBindingElement securityBindingElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement();

var secureTransport = new HttpsTransportBindingElement();
secureTransport.UseDefaultWebProxy = false;
secureTransport.ProxyAddress = new Uri("http://some-proxy");
secureTransport.ProxyAuthenticationScheme = AuthenticationSchemes.Ntlm;

binding.Elements.Add(securityBindingElement);
binding.Elements.Add(secureTransport);

var endpointAddress = new EndpointAddress("https://some-service");

var factory = new ChannelFactory<IService>(binding, endpointAddress);

// Credentials for authentication against the remote service
factory.Credentials.UserName.UserName = "serviceUser";
factory.Credentials.UserName.Password = "abc";

// Credentials for authentication against the proxy server
factory.Credentials.Windows.ClientCredential.UserName = "domain\user";
factory.Credentials.Windows.ClientCredential.Password = "xyz";

var client = factory.CreateChannel();
client.CallMethod();
//使用为客户端配置的CustomBinding的示例服务调用
//基于作为消息一部分发送的用户名和密码的身份验证。
var binding=新的CustomBinding();
TransportSecurityBindingElement securityBindingElement=securityBindingElement.CreateUserNameOverTransportBindingElement();
var secureTransport=新的HttpsTransportBindingElement();
secureTransport.UseDefaultWebProxy=false;
secureTransport.ProxyAddress=新Uri(“http://some-proxy");
secureTransport.ProxyAuthenticationScheme=AuthenticationSchemes.Ntlm;
binding.Elements.Add(securityBindingElement);
binding.Elements.Add(secureTransport);
var endpointAddress=新的端点地址(“https://some-service");
var factory=新的ChannelFactory(绑定,端点地址);
//针对远程服务进行身份验证的凭据
factory.Credentials.UserName.UserName=“serviceUser”;
factory.Credentials.UserName.Password=“abc”;
//针对代理服务器进行身份验证的凭据
factory.Credentials.Windows.ClientCredential.UserName=“域\用户”;
factory.Credentials.Windows.ClientCredential.Password=“xyz”;
var client=factory.CreateChannel();
client.CallMethod();