none
How to use HTTP proxy authentication in WCF

    Question

  • I have the following way of invoking a service method in client code:-

     

    IService proxy = ChannelFactory<IService>.CreateChannel (new WSHttpBinding (), endpoint);
    proxy.MyMethod ();

     

    I don't have any app.config file, and I don't use SvcUtil.  Note that 'endpoint' has already been created and set up correctly.

     

    This will work, but if I'm accessing a resource that requires going through our HTTP proxy server, it will fail.  WSHttpBinding will use the default web proxy by default, but that isn't enough.  The call to 'MyMethod' will give me an HTTP 407 Proxy Authentication Required failure.

     

    How do I add credentials in code (no app.config stuff)?  Ideally I would like the credentials to be picked up automatically; i.e. use the same settings that are used by IE.

     

    WCF's credentials handling has me confused...

     

    TIA

     

    Andrew

     

    Monday, December 03, 2007 5:31 PM

Answers

  • The answer was soooo simple; finding the answer took all morning!  The answer also has nothing to do with WCF, by the way, but everything to do with System.Net.

     

    System.Net's default web proxy does not have the UseDefaultCredentials flag switched on by default.  It's false.  Creating a new WebProxy object with the flag set to true is problematical... it turns out to be quite hard to find out the address and port of the default web proxy at runtime, because this is essentially a dynamic thing, and not (as previously thought in the .NET 1.x era) a static thing.

     

    So by far the best thing to do is use your app's App.Config file (if, like me, you have a client-side app) to tell System.Net to use the default credentials for authentication to an HTTP proxy server.  Whack in this:-

     

    <system.net>

      <defaultProxy

        useDefaultCredentials="true" >

      </defaultProxy>

    </system.net>

     

    Overall your App.config file might look like this:-

     

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

      <system.net>

        <defaultProxy

          useDefaultCredentials="true" >

        </defaultProxy>

      </system.net>

     

      <appSettings>

        <!-- your own app settings here -->

      </appSettings>

    </configuration>

     

    ----

     

    There you have it!  Here's an excellent reference that helped me sort it out:-

     

    http://sticklebackplastic.com/2007/1/26/Poxy+proxies.aspx

     

     

    Andrew

     

    Tuesday, December 04, 2007 12:16 PM

All replies

  •  

    Hey Andrew

     

    same problem applies to me, app.config stuff  not required while calling wcf service method in client side

    do u have a solutions, please help me out toooo

     

    thanks

     

    syed

    Monday, December 03, 2007 6:14 PM
  • SourceGear Vault can auto-detect the default web proxy's IP address and port number, plus the logged-in user's username, password and domain - all from ASMX.  So it must be possible in WCF.  If some kind MS staffer can indicate how you do it, I'll be on my way.  Thanks.

     

    Andrew

     

     

    Monday, December 03, 2007 10:48 PM
  • The answer was soooo simple; finding the answer took all morning!  The answer also has nothing to do with WCF, by the way, but everything to do with System.Net.

     

    System.Net's default web proxy does not have the UseDefaultCredentials flag switched on by default.  It's false.  Creating a new WebProxy object with the flag set to true is problematical... it turns out to be quite hard to find out the address and port of the default web proxy at runtime, because this is essentially a dynamic thing, and not (as previously thought in the .NET 1.x era) a static thing.

     

    So by far the best thing to do is use your app's App.Config file (if, like me, you have a client-side app) to tell System.Net to use the default credentials for authentication to an HTTP proxy server.  Whack in this:-

     

    <system.net>

      <defaultProxy

        useDefaultCredentials="true" >

      </defaultProxy>

    </system.net>

     

    Overall your App.config file might look like this:-

     

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

      <system.net>

        <defaultProxy

          useDefaultCredentials="true" >

        </defaultProxy>

      </system.net>

     

      <appSettings>

        <!-- your own app settings here -->

      </appSettings>

    </configuration>

     

    ----

     

    There you have it!  Here's an excellent reference that helped me sort it out:-

     

    http://sticklebackplastic.com/2007/1/26/Poxy+proxies.aspx

     

     

    Andrew

     

    Tuesday, December 04, 2007 12:16 PM
  • I had a similar problem (trying to access a WCF from a WPF browser app through a http proxy) so I tried this; if I add this bit to the app.config file I get "Insufficient permissions for setting the configuration section 'defaultProxy'" - as soon as I try to access the WCF. This may be due to partial trust of the WPF browser app. Any Ideas? I am currently on .NET framework 4.
    Friday, July 16, 2010 2:04 PM
  • Good man! sorted, thank you.
    lf
    Monday, August 16, 2010 9:08 AM
  • Hey Andrew,

     

    Thanks alot...........

    Tuesday, January 25, 2011 1:29 PM
  • Awesome.  This solved my problem as well.  Thank you!
    Monday, July 18, 2011 7:48 PM