代码之家  ›  专栏  ›  技术社区  ›  Gareth

WCF自定义Http代理身份验证

  •  5
  • Gareth  · 技术社区  · 16 年前

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

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

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

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

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

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

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

    2 回复  |  直到 8 年前
        1
  •  14
  •   Bradley Grainger    15 年前

    如果你设置 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 包含更多详细信息。

        2
  •  2
  •   sebagomez    16 年前

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

    这个链接可能会帮助你。

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