通过EWS进行Outlook Web Access

时间:2013-11-21 16:44:29

标签: c# authentication outlook exchangewebservices

我正在尝试通过EWS连接到Exchange Web Access门户。门户网站使用基于表单的身份验证,这很难找到信息。 / EWS/Exchange.asmx页面未公开,WebDAV也没有公开exchweb / bin / auth / owaauth.dll,我无法对服务器设置进行任何更改。我的代码很简单:

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());
}

该跟踪信息丰富,因为它显示我正在使用基本身份验证,但我无法通过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>

最后,我得到的例外是未经授权的,正如预期的那样,因为Exchange服务器不支持EWS中使用的身份验证方法:

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)

我有理由相信,通过/ OWA URL访问服务器是可行的,前提是我可以弄清楚如何对指定的凭据使用表单身份验证。有没有人有关于如何做到这一点的一些参考或指示?或者我完全走错了轨道?

谢谢!

1 个答案:

答案 0 :(得分:2)

回答我自己的问题。事实证明,提供给我的URL不正确,并且他们确实有一个EWS / Exchange.asmx URL,它只对我的代码很有用。我只需要从中删除URL。 ;)