【问题标题】:How to set proxy with credentials to generated WCF client?如何使用凭据设置代理到生成的 WCF 客户端?
【发布时间】:2010-09-11 10:54:13
【问题描述】:

如果客户使用带有凭据的代理,我无法连接到我的 WCF 服务。我无法找到将凭据设置为生成的客户端代理的方法。

如果我使用网络服务,那么可以设置代理。

【问题讨论】:

    标签: wcf


    【解决方案1】:

    我不完全确定这是否是您正在寻找的东西,但您可以去。

      MyClient client = new MyClient();
      client.ClientCredentials.UserName.UserName = "u";
      client.ClientCredentials.UserName.Password = "p";
    

    【讨论】:

    • 它似乎不适用于 NTLM 代理。我需要填写 client.ClientCredentials.Windows 才能让它工作。
    • 这是一个通道凭证,不是 http 代理凭证
    • @aloneguid 在 WCF 中很确定它有双重职责。是的,这太疯狂了。
    【解决方案2】:

    我通过将 Active Directory 用户添加到应用程序池>身份而不是网络服务来解决此问题。该用户也属于有权通过代理服务器浏览 Internet 的组。还要将此用户添加到客户端主机服务器上的 IIS_WPG 组。

    在下面的代码中,第一位使用 WCF 服务对客户端进行身份验证。第二位假设将凭证传递给内部代理服务器,以便客户端调用 DMZ 服务器上的 WCF 服务。但我认为代理部分不起作用。无论如何,我要留下代码。

            // username token credentials
            var clientCredentials = new ClientCredentials();
            clientCredentials.UserName.UserName = ConfigurationManager.AppSettings["Client.Mpgs.Username"];
            clientCredentials.UserName.Password = ConfigurationManager.AppSettings["Client.Mpgs.Password"];
            proxy.ChannelFactory.Endpoint.Behaviors.Remove(typeof(ClientCredentials));
            proxy.ChannelFactory.Endpoint.Behaviors.Add(clientCredentials);
    
            // proxy credentials 
            //http://kennyw.com/indigo/143
            //http://blogs.msdn.com/b/stcheng/archive/2008/12/03/wcf-how-to-supply-dedicated-credentials-for-webproxy-authentication.aspx
            proxy.ChannelFactory.Credentials.Windows.ClientCredential = new System.Net.NetworkCredential
                                                                        (
                                                                            ConfigurationManager.AppSettings["Client.ProxyServer.Username"]
                                                                           , ConfigurationManager.AppSettings["Client.ProxyServer.Password"]
                                                                           , ConfigurationManager.AppSettings["Client.ProxyServer.DomainName"]
                                                                         );
    

    在我的 web.config 中,我使用了以下内容,

    <system.net>
        <defaultProxy useDefaultCredentials="true">
            <proxy usesystemdefault="True" proxyaddress="http://proxyServer:8080/" bypassonlocal="False" autoDetect="False"  />     </defaultProxy>
    </system.net>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_ITest" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
                    <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/>
                    <security mode="TransportWithMessageCredential">
                        <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
                        <message clientCredentialType="UserName" negotiateServiceCredential="true" algorithmSuite="Default"/>
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://wcfservice.organisation.com/test/test.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITest" contract="Test.Test" name="WSHttpBinding_ITest"/>
        </client>
    </system.serviceModel>
    

    上面的代码可以在我的本地机器上运行。当我将代码上传到开发服务器时,它不起作用。我查看了代理服务器日志,如下所示,

    2011-06-14 05:21:10 2 11.11.11.11 - - authentication_failed DENIED “组织/财务” - 407 TCP_DENIED CONNECT - tcp wcfservice.organisation.com 443 / - - - 11.11.11.11 612 161 -

    2011-06-14 05:21:10 6 11.11.11.152 ServerName$ - policy_denied DENIED “组织/财务” - 403 TCP_DENIED CONNECT - tcp wcfservice.organisation.com 443 / - - - 11.11.11.205 185 361 -

    我们的智能系统管理员 DF 将 Active Directory 用户添加到应用程序池>身份而不是网络服务。该用户也属于有权通过代理服务器浏览 Internet 的组。还要将此用户添加到客户端主机服务器上的 IIS_WPG 组。

    这对我有用。

    【讨论】:

      【解决方案3】:

      不确定这是否是您要查找的内容,但以下是使用客户端凭据进行身份验证的工作代码示例。

          Dim client As ProductServiceClient = New ProductServiceClient("wsHttpProductService")
          client.ClientCredentials.UserName.UserName = "username"
          client.ClientCredentials.UserName.Password = "password"
          Dim ProductList As List(Of Product) = client.GetProducts()
          mView.Products = ProductList
          client.Close()
      

      【讨论】:

        猜你喜欢
        • 2013-07-31
        • 2017-03-22
        • 2012-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多