我有一个共享邮箱,我正在尝试在 EWS 中连接到该邮箱。我能够从我的 Outlook 2016 实例很好地访问邮箱,而无需提供凭据。当我尝试通过 EWS 访问它时,出现以下错误:
"请求失败。远程服务器返回错误:(401( 未经授权。
我正在我的 AD 帐户下运行此代码,该帐户有权访问此邮箱。为什么在这种情况下我无法访问?下面是有问题的代码。
service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
service.UseDefaultCredentials = true;
var shared_mailbox = new FolderId(WellKnownFolderName.Inbox, "mailbox@mydomain.com);
FindItemsResults<Item> results = service.FindItems(shared_mailbox, dateTimeFilter, view); //401 is thrown here.
有没有人看到任何错误或缺失?
我建议你考虑使用新式身份验证(这也是Outlook应该使用的(。若要使用当前登录的凭据,需要使用 MSAL 库和以下 https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/Integrated-Windows-Authentication
获得令牌后,请使用
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2016);
service.Url = new Uri("https://outlook.office365.com/ews/exchange.asmx");
service.Credentials = new OAuthCredentials(TokenResult.AccessToken);
service.HttpHeaders.Add("X-AnchorMailbox", MailboxName);
基本身份验证将于明年消失,因此将来使用 OAuth 将是一项艰巨的要求。