user3018330
user3018330

Reputation: 21

Outlook Web Access through EWS

I'm trying to connect to an Exchange Web Access portal through EWS. The portal uses forms-based authentication, which is proving difficult to find information for. The /EWS/Exchange.asmx page is not exposed, nor is exchweb/bin/auth/owaauth.dll for WebDAV, and I can't make any changes to the server setup. My code is straightforward:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

try
{
    service.Url = new Uri("https://server");

    service.TraceEnabled = true;
    service.Credentials = new WebCredentials("username", "password");
    service.UseDefaultCredentials = false;

    service.FindFolders(
        (FolderId)WellKnownFolderName.Root,
        new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep });
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}

The trace is informative in that it shows I'm using basic authentication, but I've been unable to find any information on changing that through the EWS API:

<Trace Tag="EwsRequestHttpHeaders" Tid="1" Time="2013-11-21 16:14:04Z">
POST / HTTP/1.1
Content-Type: text/xml; charset=utf-8
Accept: text/xml
User-Agent: ExchangeServicesClient/15.00.0516.014
Accept-Encoding: gzip,deflate
</Trace>

<Trace Tag="EwsRequest" Tid="1" Time="2013-11-21 16:14:04Z" Version="15.00.0516.014">
  <?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
      <t:RequestServerVersion Version="Exchange2007_SP1" />
      <t:TimeZoneContext>
        <t:TimeZoneDefinition Id="Eastern Standard Time" />
      </t:TimeZoneContext>
    </soap:Header>
    <soap:Body>
      <m:FindFolder Traversal="Deep">
        <m:FolderShape>
          <t:BaseShape>AllProperties</t:BaseShape>
        </m:FolderShape>
        <m:IndexedPageFolderView MaxEntriesReturned="2147483647" Offset="0" BasePoint="Beginning" />
        <m:ParentFolderIds>
          <t:DistinguishedFolderId Id="root" />
        </m:ParentFolderIds>
      </m:FindFolder>
    </soap:Body>
  </soap:Envelope>
</Trace>

<Trace Tag="EwsResponseHttpHeaders" Tid="1" Time="2013-11-21 16:14:05Z">
HTTP/1.1 401 Unauthorized
Connection: Keep-Alive
X-UA-Compatible: IE=EmulateIE7
Content-Length: 1293
Content-Type: text/html
Date: Thu, 21 Nov 2013 16:14:05 GMT
Set-Cookie: OutlookSession=996071898c834931b682a258b41187ac; path=/; HttpOnly
Server: Microsoft-IIS/7.5
WWW-Authenticate: Basic
X-Powered-By: ASP.NET
</Trace>

Finally, the exception I get is unauthorized, as expected since the authentication method used in EWS isn't supported by the Exchange server:

Microsoft.Exchange.WebServices.Data.ServiceRequestException: The request failed. The remote server returned an error: (401) Unauthorized. ---> System.
Net.WebException: The remote server returned an error: (401) Unauthorized.
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.GetResponse()
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)
   --- End of inner exception stack trace ---
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request)
   at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
   at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalFindFolders(IEnumerable`1 parentFolderIds, SearchFilter searchFilter, FolderView view, ServiceErrorHandling errorHandlingMode)
   at Microsoft.Exchange.WebServices.Data.ExchangeService.FindFolders(FolderId parentFolderId, FolderView view)

I'm reasonably sure that accessing the server through the /OWA URL will work, provided I can figure out how to use forms authentication for the specified credentials. Does anyone have some references or pointers on how to do that? Or am I on the wrong track entirely?

Thanks!

Upvotes: 0

Views: 2755

Answers (1)

user3018330
user3018330

Reputation: 21

Yay for answering my own question. It turns out that the URL provided to me was not correct, and they did indeed have an EWS/Exchange.asmx URL that worked just peachy with my code. I just needed to weedle the URL out of them. ;)

Upvotes: 2

Related Questions