WCF自定义Http代理身份验证

时间:2008-10-09 12:18:44

标签: wcf authentication proxy

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

我在stackoverflow上找到了这个答案:How to set proxy with credentials to generated WCF client?,但我有一个复杂的问题,我正在验证的服务使用自己的身份验证,所以我必须使用两组凭据(一个通过代理,另一个通过服务进行身份验证)

我正在使用其他问题的答案中描述的技术来提供服务凭据。 e.g。

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

我可以使用以下内容设置代理的地址:

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

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

2 个答案:

答案 0 :(得分:13)

如果将WebRequest.DefaultWebProxy属性设置为具有凭据的新WebProxy,则WCF将其用于它所做的所有HTTP请求。 (这将影响应用程序使用的所有HttpWebRequests,除非明确覆盖)。

// 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),
};

我的blog post on proxy servers包含更多详细信息。

答案 1 :(得分:2)

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

此链接可能会帮助您。

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