Office 365 - EWS托管API 2.0模拟401未授权/ 503不可用

时间:2016-06-16 20:31:30

标签: c# office365 impersonation ews-managed-api

private ExchangeService connectToEWS(string email, bool impersonate = false) {
   WebCredentials credentials = new WebCredentials(username, password, domain);
   ExchangeVersion version = (ExchangeVersion)exchangeVersion;
   ExchangeService service = new ExchangeService(version) {
      Credentials = credentials,
      Url = new Uri("https://outlook.office365.com/ews/exchange.asmx")
   };
   if (impersonate) {
      service.HttpHeaders.Add("X-AnchorMailbox", email);
      service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, email);                      service.CookieContainer = new CookieContainer();
   }
   service.Timeout = int.MaxValue;
}

使用上面的代码创建ExchangeSession和

ExchangeService service = connectToEWS(email, true);
FolderId msgFolderRoot = WellKnownFolderName.MsgFolderRoot;
FolderView view = new FolderView(int.MaxValue);
view.PropertySet = PropertySet.IdOnly;
var result = service.FindFolders(msgFolderRoot, view);

我在FindFolders电话上收到401未经授权的例外。如果我使用仅接受smtp和密码的WebCredentials的其他构造函数,则会得到503服务器不可用的异常。当我不使用模拟(代理访问)时,不会抛出任何异常,我可以获取文件夹列表,但我需要能够支持模拟。

我已将ApplicationImpersonation角色添加到服务帐户(第一个代码块中的用户名)。是否还需要扮演其他角色?我在EWS模拟文档中找不到任何内容。关于这个问题的每个其他线程都指向UPN使用而不是smtp,但我很确定这个smtp与UPN相同。我实际上并不知道在o365内的哪个地方。

0 个答案:

没有答案