WPF proxy authentication

13624
15
01-24-2012 10:42 AM
LorenCress
Esri Contributor
Google tells me that I'm not the only one facing this problem: we are building a WPF application using the Esri WPF API, but in order to consume REST services we have to go through a proxy.  That would be fine, except that the proxy requires authentication.  All of the WebClient libraries support the Proxy object, which in turn supports a Credentials object.  The Esri API classes, though, only offer me a "ProxyURL" string, and a separate Credentials object, which doesn't appear to do anything for the proxy; I wouldn't expect it to.  That object seems to be for providing creds to the REST endpoint in a secured ArcGIS Server setting.

"ProxyURL" is obviously for supporting a "proxy page", which is not applicable for our situation - that's not what we're doing.  We'd just like to consume simple, insecure maps and services, like ArcGIS Online, through a forward web proxy.

One workaround that we've found is to build - in the application itself - our own tiny webserver/proxy that uses WebClient to pass http requests through to the company proxy.  That seems very kludgy, though. How can we get through the proxy without writing our own WebClient-based proxy into our application?
15 Replies
RickSipin
New Contributor
Loren,
Pretty awesome of you to post your own answer to this obvious omission on the part of the API.  Thanks for doing that, and know that ESRI support is now pointing those of us who discover this glaring problem, to your solution.

-Rick
0 Kudos
LorenCress
Esri Contributor
No problem, glad I could help.  I know we're not the only ones facing this.

FYI, since I posted the code, I've refined it somewhat for our needs.  We created another application for which I used the same basic code, but we also needed HTTPS, request interception, plus a couple of other bells & whistles.

My point is that if you have issues getting the posted code to work, shoot me a message and I can dig up the updated code for you.

Plus: LOL.
0 Kudos
JohnDye
Occasional Contributor III

Hi Loren,

I seem to be running into this same issue. Would you mind sharing your updated code with me?

0 Kudos
DominiqueBroux
Esri Frequent Contributor
You can set WebRequest.DefaultWebProxy at the startup of your application then this default proxy will be used by the ArcGIS WPF API.

Example of code for using the OS proxy settings with a particular credential:

IWebProxy webProxy = WebRequest.GetSystemWebProxy();
webProxy.Credentials = new NetworkCredential("<username>", "<password>");
WebRequest.DefaultWebProxy = webProxy;


If you want to use a proxy that is not the default proxy, you can use code like:
WebRequest.DefaultWebProxy = new WebProxy("http://<myProxyHost>:<myProxyPort>", true) // secong arg=true to bypass on local
{
    Credentials = new NetworkCredential("<username>", "<password>")
};


Note: to get the WPF Runtime local services working with the proxy, you have to bypass the proxy server for the local addresses. If you use the OS proxy settings, you have to check the option �??Bypass proxy server for local address'.


For more complex scenario where you would like to define different web proxies by server. You can use code like:

WebRequest.RegisterPrefix("http://www.arcgis.com", new WebRequestCreateThroughProxy());


public class WebRequestCreateThroughProxy : IWebRequestCreate
{
    private static readonly WebProxy _webProxy;

    static WebRequestCreateThroughProxy()
    {
        _webProxy = new WebProxy("http://<myProxyHost>:<myProxyPort>", true)
        {
            Credentials = new NetworkCredential("<username>", "<password>")
        };
    }

    public WebRequest Create(Uri uri)
    {
        var webRequest = WebRequest.CreateHttp(uri);
        webRequest.Proxy = _webProxy;
        return webRequest;
    }
}

With this code, only the requests to arcgis.com will go through the proxy.


Note: if your proxy is secured with Windows Authentication, you can also get working your WPF application by defining the defaultProxy in the system.net section of your app.config file (without any other code)

<system.net>
     <defaultProxy enabled="true" useDefaultCredentials="true"/>
</system.net>
RuiShen
New Contributor III
Get some information from our user, hope it can help others:

private void Application_Startup(object sender, StartupEventArgs e)
        {
          
            IWebProxy webProxy = WebRequest.GetSystemWebProxy();

        //instead of using clear text credential ,get that from CredentialCache
            //webProxy.Credentials = new NetworkCredential("username", "password");
            webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
            WebRequest.DefaultWebProxy = webProxy;

            // Initialize the ArcGIS Runtime before any components are created.
            try
            {
                ArcGISRuntime.Initialize();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());

                // Exit application
                this.Shutdown();
            }

        }
0 Kudos
vinobieze
New Contributor
hello,
am looking for a developer who will do a web proxy application for me.
The application will have a username and password where by a user will be issued his id before he can use the web proxy application.
I prefer using radius authentication system.the Application will receive response from a server which i ll give the developer.
Please is for my project and i will pay.
0 Kudos