如何使用凭据设置代理以生成WCF客户端?

时间:2021-01-01 15:25:35

I have a problem to connect to my WCF service if customer is using proxy with credentials. I'm unable to find the way to set credential to generated client proxy.

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

If I use the web service, then it is possible to set proxy.

如果我使用Web服务,则可以设置代理。

3 个解决方案

#1


3  

I'm not entirely sure if this is what you are looking for but here you go.

我不完全确定这是你正在寻找的,但是你走了。

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

#2


2  

I resolved this by adding an Active Directory user to the Application Pool>Identity instead of network services. This user is also in a group who has permission to browse internet through proxy server. Also add this user to the IIS_WPG group on the client host server.

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

In the code below the first bit authenticate the client with the WCF service. The second bit suppose to pass the crendentials to internal proxy server so that the client call a WCF service on the DMZ server. But I don't think the proxy part is works. I am leaving the code anyway.

在下面的代码中,第一位使用WCF服务对客户端进行身份验证。第二位假设将crendentials传递给内部代理服务器,以便客户端在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"]
                                                                     );

In my web.config I have used the following,

在我的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>

The above code works from my local machine. When I upload the code to a dev server it does not work. I looked at the proxy server logs and it shows below,

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

2011-06-14 05:21:10 2 11.11.11.11 - - authentication_failed DENIED "Organisation/Finance" - 407 TCP_DENIED CONNECT - tcp wcfservice.organisation.com 443 / - - - 11.11.11.11 612 161 -

2011-06-14 05:21:10 2 11.11.11.11 - - authentication_failed DENIED“Organization / Finance” - 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 "Organisation/Finance" - 403 TCP_DENIED CONNECT - tcp wcfservice.organisation.com 443 / - - - 11.11.11.205 185 361 -

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

Our smart system administrator DF added a Active Directory user to the Application Pool>Identity instead of network services. This user is also in a group who has permission to browse internet through proxy server. Also add this user to the IIS_WPG group on the client host server.

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

This worked for me.

这对我有用。

#3


1  

Not sure if this is what you are looking for but the below is a working code sample to authenticate using the client credentials.

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

    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()

#1


3  

I'm not entirely sure if this is what you are looking for but here you go.

我不完全确定这是你正在寻找的,但是你走了。

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

#2


2  

I resolved this by adding an Active Directory user to the Application Pool>Identity instead of network services. This user is also in a group who has permission to browse internet through proxy server. Also add this user to the IIS_WPG group on the client host server.

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

In the code below the first bit authenticate the client with the WCF service. The second bit suppose to pass the crendentials to internal proxy server so that the client call a WCF service on the DMZ server. But I don't think the proxy part is works. I am leaving the code anyway.

在下面的代码中,第一位使用WCF服务对客户端进行身份验证。第二位假设将crendentials传递给内部代理服务器,以便客户端在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"]
                                                                     );

In my web.config I have used the following,

在我的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>

The above code works from my local machine. When I upload the code to a dev server it does not work. I looked at the proxy server logs and it shows below,

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

2011-06-14 05:21:10 2 11.11.11.11 - - authentication_failed DENIED "Organisation/Finance" - 407 TCP_DENIED CONNECT - tcp wcfservice.organisation.com 443 / - - - 11.11.11.11 612 161 -

2011-06-14 05:21:10 2 11.11.11.11 - - authentication_failed DENIED“Organization / Finance” - 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 "Organisation/Finance" - 403 TCP_DENIED CONNECT - tcp wcfservice.organisation.com 443 / - - - 11.11.11.205 185 361 -

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

Our smart system administrator DF added a Active Directory user to the Application Pool>Identity instead of network services. This user is also in a group who has permission to browse internet through proxy server. Also add this user to the IIS_WPG group on the client host server.

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

This worked for me.

这对我有用。

#3


1  

Not sure if this is what you are looking for but the below is a working code sample to authenticate using the client credentials.

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

    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()