c# - 将 EWS 托管 Api 与 Office 365 Api 结合使用

标签 c# oauth-2.0 exchangewebservices office365

我正在尝试通过 Azure AD 将 EWS 托管 Api 与 Office 365 Api 结合使用。到目前为止我已经完成了以下任务。

  • 我在 Azure AD 中拥有管理员权限。
  • 我已在 Azure AD 中成功注册我的应用程序。
  • 我从 Azure AD 获取了客户端 ID、应用 key 和资源 ID。
  • 我已按照 Jason 的建议启用了“对用户邮箱具有完全访问权限”。
  • 我已成功创建 MVC5 Web 应用程序。
  • 我关注了 Jeremy 的这篇博文。

这是我关注的博客的链接: http://www.jeremythake.com/2014/08/using-the-exchange-online-ews-api-with-office-365-api-via-azure-ad/#comment-280653

我的 Controller 中的代码:

   var outlookClient = await AuthHelper.EnsureOutlookServicesClientCreatedAsync("Mail");

    IPagedCollection<IMessage> messagesResults = await     outlookClient.Me.Messages.ExecuteAsync();

    string messageId = messagesResults.CurrentPage[0].Id;
    string tokenx = AuthHelper.GetSessionToken();
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
    service.HttpHeaders.Add("Authorization", "Bearer " + tokenx);
    service.PreAuthenticate = true;
    service.SendClientLatencies = true;
    service.EnableScpLookup = false;
    service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

    ExFolder rootfolder = ExFolder.Bind(service, WellKnownFolderName.MsgFolderRoot);

已编辑:我成功获取 accessToken 并使用它对 EWS 托管 Api 进行调用,但失败并出现 403:Forbidden 异常。我们将非常感谢您的帮助。

致以诚挚的问候,

最佳答案

Jason Johnston 帮助我解决了问题。 链接:
Office 365 / EWS Authentication using OAuth: The audience claim value is invalid

我检查了 EWS 跟踪,发现 EWS 提示 token 无效和权限不足。我将应用程序重新注册到 Azure AD 并启用了对邮箱的完全访问权限。

我在下面的代码中对此进行了评论。

//var outlookClient = await AuthHelper.EnsureOutlookServicesClientCreatedAsync("Mail");
        //try
        //{
        //    IPagedCollection<IMessage> messagesResults = await outlookClient.Me.Messages.ExecuteAsync();

        //    string messageId = messagesResults.CurrentPage[0].Id;
        //}
        //catch
        //{
        //    System.Diagnostics.Debug.WriteLine("Something bad happened. !!");
        //}  

我从下面的链接示例中获取访问 token 。 https://github.com/OfficeDev/Office-365-APIs-Starter-Project-for-ASPNETMVC

这是 Controller 的完整代码,它执行身份验证的主要任务。

string resourceUri = "https://outlook.office365.com";
        var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
        var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
        AuthenticationContext authContext = new AuthenticationContext(Settings.Authority, new NaiveSessionCache(signInUserId));
        string tokenx = await AuthHelper.AcquireTokenAsync(authContext, resourceUri, Settings.ClientId, new UserIdentifier(userObjectId,UserIdentifierType.UniqueId));

        System.Diagnostics.Debug.WriteLine("Token:" + tokenx);

            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
            service.TraceListener = new EwsTrace();
            service.TraceEnabled = true;
            service.TraceFlags = TraceFlags.All;
            service.HttpHeaders.Add("Authorization", "Bearer " + tokenx);
            service.PreAuthenticate = true;
            service.SendClientLatencies = true;
            service.EnableScpLookup = false;
            service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

            ExFolder rootfolder = ExFolder.Bind(service, WellKnownFolderName.MsgFolderRoot);

        Console.WriteLine("The " + rootfolder.DisplayName + " has " + rootfolder.ChildFolderCount + " child folders.");

我注意到的重要一点是,我无法使用相同的 token 来访问 Office365 api 和 EWS 托管 Api,因为 EWS 可支持完整邮箱访问,而 Office365 则不能。我请求开发人员确认这一点,也许我做错了什么,但我的问题现在已经解决了。

关于c# - 将 EWS 托管 Api 与 Office 365 Api 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27077529/

相关文章:

c# - 当我将 OrderBy 放在查询开头时,为什么 EF 会添加大量未使用的 OUTER APPLY 子句

c# - 来自另一个解决方案的引用项目

html - Azure 与 OAuth 2 - google+ 登录信息和注销按钮

python - 决定何时使用 Python Social Auth 刷新 OAUTH2 token

java - 使用 IMAP 或 Exchange Web 服务从 GWT 应用程序的 Exchange Server 获取电子邮件的最快方法是什么?

c# - 使用 RFC_READ_TEXT 从销售订单中检索文本

c# - 无法在 GridView 中持久化数据

android - Chrome 自定义标签不会在重定向时关闭

exchange-server - 什么导致 OnSubscriptionError 在 EWS API 的 StreamingSubscriptionConnection 中被触发?

c++ - gSoap Exchange Web 服务连接