Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set proxy with credentials to generated WCF client?

Tags:

wcf

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.

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

like image 452
Dusan Kocurek Avatar asked Sep 19 '08 20:09

Dusan Kocurek


2 Answers

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";
like image 182
Pawel Pabich Avatar answered Oct 02 '22 18:10

Pawel Pabich


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.

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.

        // 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,

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

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.

This worked for me.

like image 44
Diganta Kumar Avatar answered Oct 02 '22 17:10

Diganta Kumar