5

是否可以为 WCF 提供自定义代理地址和自定义凭据?

我在 stackoverflow 上找到了这个答案:如何使用凭据设置代理到生成的 WCF 客户端?,但我有一个复杂的问题,我正在验证的服务使用自己的身份验证,所以我必须使用两组凭据(一组通过代理,另一组通过服务进行身份验证)

我正在使用另一个问题的答案中描述的技术来提供服务凭据。例如

client.ClientCredentials.UserName.UserName = username;
client.ClientCredentials.UserName.Password = password;

我可以使用以下方式设置代理的地址:

(client.Endpoint.Binding as WSHttpBinding).ProxyAddress = ...;

如何设置有效的两组凭据?(注意:代理和实际服务的凭据不同!)还要注意,代理详细信息不一定是默认系统代理详细信息。

4

2 回答 2

14

如果将WebRequest.DefaultWebProxy属性设置为具有凭据的新 WebProxy,WCF 将使用它来处理它发出的所有 HTTP 请求。(这将影响应用程序使用的所有 HttpWebRequest,除非显式覆盖)。

// get this information from the user / config file / etc.
Uri proxyAddress;
string userName;
string password;

// set this before any web requests or WCF calls
WebRequest.DefaultWebProxy = new WebProxy(proxyAddress)
{
    Credentials = new NetworkCredential(userName, password),
};

关于代理服务器的博文包含更多详细信息。

于 2010-01-21T18:05:33.530 回答
2

您设置的客户端凭据很好,以便对您的服务进行身份验证。
对于代理身份验证,您需要使用 HttpTransportSecurity.ProxyCredentials。

这个链接可能会帮助你。

http://msdn.microsoft.com/en-us/library/system.servicemodel.httptransportsecurity.proxycredentialtype.aspx

于 2008-10-09T13:02:08.153 回答