ews-managed-api icon indicating copy to clipboard operation
ews-managed-api copied to clipboard

ExchangeService.AutodiscoverUrl() for O365 fails when proxy authentication is required

Open RichardHeatley opened this issue 10 years ago • 1 comments

Hello,

I'm currently trying to add support for proxies to my application and once I configure the proxy to require authentication my calls to ExchangeService.AutodiscoverUrl() fail. The proxy is TMG 2010 and I'm trying to connect to an Office 365 Calendar via EWS using version 2.2 of the API obtained via NuGet.

I configure the service as follows:

this.service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
var proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = new NetworkCredential(@"MYDOMAIN\myuser", "mypassword");
this.service.WebProxy = proxy;

But

this.service.AutodiscoverUrl(this.email, this.ValidateRedirectionUrlCallback);

fails with

Microsoft.Exchange.WebServices.Data.AutodiscoverLocalException: The Autodiscover service couldn't be located.
   at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.InternalGetLegacyUserSettings[TSettings](String emailAddress, List`1 redirectionEmailAddresses, Int32& currentHop) in lib\Microsoft.Exchange.WebServices.Data\Autodiscover\AutodiscoverService.cs:line 646
   at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.GetLegacyUserSettings[TSettings](String emailAddress) in lib\Microsoft.Exchange.WebServices.Data\Autodiscover\AutodiscoverService.cs:line 432
   at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.InternalGetLegacyUserSettings(String emailAddress, List`1 requestedSettings) in lib\Microsoft.Exchange.WebServices.Data\Autodiscover\AutodiscoverService.cs:line 828
   at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.GetUserSettings(String userSmtpAddress, UserSettingName[] userSettingNames) in lib\Microsoft.Exchange.WebServices.Data\Autodiscover\AutodiscoverService.cs:line 1694
   at Microsoft.Exchange.WebServices.Data.ExchangeService.GetAutodiscoverUrl(String emailAddress, ExchangeVersion requestedServerVersion, AutodiscoverRedirectionUrlValidationCallback validateRedirectionUrlCallback) in lib\Microsoft.Exchange.WebServices.Data\Core\ExchangeService.cs:line 5028
   at Microsoft.Exchange.WebServices.Data.ExchangeService.AutodiscoverUrl(String emailAddress, AutodiscoverRedirectionUrlValidationCallback validateRedirectionUrlCallback) in lib\Microsoft.Exchange.WebServices.Data\Core\ExchangeService.cs:line 4985
   at LyncRs.EwsEndpoint.ThreadMain() in EwsEndpoint.cs:line 149

In this case, I know what the Autodiscovered URL will be and if I provide it to the ExchangeService then it connects just fine. This demonstrates the WebProxy is configured correctly; if I ommit it or set incorrect credentials it fails.

Examining the EWS code, the AutodiscoverService never configures the Proxy property of IEwsHttpWebRequest objects. My understanding is these requests will get created with the Proxy property set to null which will result in the system proxy being used. Therefore it is entirely possible for these requests to be proxied so it seems reasonable to assume the ExchangeService WebProxy should be used? FYI I see the requests arrive at the proxy and get rejected with 407 as they are not authenticated. Sadly it looks like the default WebProxy object doesn't automagically grab credentials from the Credential Manager? If it did then I would never have noticed this problem.

So, is this all a bug? I have a small patch that makes it work for me, is this bad/naughty in anyway?

Index: lib/Microsoft.Exchange.WebServices.Data/Autodiscover/AutodiscoverService.cs
===================================================================
--- lib/Microsoft.Exchange.WebServices.Data/Autodiscover/AutodiscoverService.cs (revision 54799)
+++ lib/Microsoft.Exchange.WebServices.Data/Autodiscover/AutodiscoverService.cs (working copy)
@@ -296,6 +296,11 @@
             request.AllowAutoRedirect = false;
             request.PreAuthenticate = false;

+            if (this.WebProxy != null)
+            {
+              request.Proxy = this.WebProxy;
+            }
+
             IEwsHttpWebResponse response = null;

             try
@@ -1366,6 +1371,11 @@
                 request.PreAuthenticate = false;
                 request.UseDefaultCredentials = false;

+                if (this.WebProxy != null)
+                {
+                  request.Proxy = this.WebProxy;
+                }
+
                 IEwsHttpWebResponse response = null;

                 try
Index: lib/Microsoft.Exchange.WebServices.Data/Core/ExchangeService.cs
===================================================================
--- lib/Microsoft.Exchange.WebServices.Data/Core/ExchangeService.cs     (revision 54799)
+++ lib/Microsoft.Exchange.WebServices.Data/Core/ExchangeService.cs     (working copy)
@@ -5021,7 +5021,8 @@
             AutodiscoverService autodiscoverService = new AutodiscoverService(this, requestedServerVersion)
             {
                 RedirectionUrlValidationCallback = validateRedirectionUrlCallback,
-                EnableScpLookup = this.EnableScpLookup
+                EnableScpLookup = this.EnableScpLookup,
+                WebProxy = this.WebProxy
             };

             GetUserSettingsResponse response = autodiscoverService.GetUserSettings(

The changes to the first file, set the Proxy property on outgoing requests. The check for "!= null" seems unnecessary to me but is consistent with the other usage of WebProxy in the file. The changes to the second file, copy the WebProxy from the ExchangeService to the newly created AutodiscoverService.

If it is helpful here are the trace outputs for the EWS library as it is (NotWorking.txt) and my patched version (Patched.txt)

The only vaguely related issue I could find was https://github.com/OfficeDev/ews-managed-api/issues/26 - apologies if this overlaps...

I hope that all makes sense! Kind regards, Richard.

RichardHeatley avatar Nov 18 '15 16:11 RichardHeatley

Has this gone anywhere? I've possibly come across the same issue when testing proxy server environments and despite setting the proxy, the autodiscover fails. Has this fix been merged anywhere?

And is this commit related?

https://github.com/OfficeDev/ews-managed-api/commit/d6f78f91145ca2451e7c67ea971ef3300183f0d4

tjmoore avatar Jun 14 '18 12:06 tjmoore