【问题标题】:How to restrict mailbox access for office 365 app如何限制 Office 365 应用程序的邮箱访问
【发布时间】:2021-07-11 07:15:50
【问题描述】:

我正在尝试弄清楚如何限制应用程序可以访问的邮箱。

我已遵循本指南并使用纯应用身份验证:https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth

根据文档,我必须设置“full_access_as_app”权限。 但是信息文本指出: “允许应用通过 Exchange Web 服务获得对所有邮箱的完全访问权限,而无需登录用户。”

我可以读取邮箱,但我想限制我的应用程序可以访问的邮箱。 谁能指出我正确的方向?

谢谢。

我的代码:

   static async System.Threading.Tasks.Task Main(string[] args)
    {
        // Using Microsoft.Identity.Client 4.22.0
        var cca = ConfidentialClientApplicationBuilder
            .Create(ConfigurationManager.AppSettings["appId"])
            .WithClientSecret(ConfigurationManager.AppSettings["clientSecret"])
            .WithTenantId(ConfigurationManager.AppSettings["tenantId"])
            .Build();

        var ewsScopes = new string[] { "https://outlook.office365.com/.default" };

        try
        {
            var authResult = await cca.AcquireTokenForClient(ewsScopes)
                .ExecuteAsync();

            // Configure the ExchangeService with the access token
            var ewsClient = new ExchangeService
            {
                Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx"),
                Credentials = new OAuthCredentials(authResult.AccessToken),
                ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "emailaddress@domain.com")
            };

            var mailbox = new Mailbox("emailaddress@domain.com");
            var folderId = new FolderId(WellKnownFolderName.Inbox, mailbox);

            var inbox = Folder.Bind(ewsClient, folderId);

            if (inbox != null)
            {
                FindItemsResults<Item> items = inbox.FindItems(new ItemView(100));

                foreach (var item in items)
                {
                    Console.WriteLine(item.Subject);
                }
            }
        }
        catch (MsalException ex)
        {
            Console.WriteLine($"Error acquiring access token: {ex}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex}");
        }

        if (System.Diagnostics.Debugger.IsAttached)
        {
            Console.WriteLine("Hit any key to exit...");
            Console.ReadKey();
        }
    }

【问题讨论】:

  • 这基本上是一个support question,而不是一个编程问题,这些都是题外话。您必须询问 Microsoft 如何配置该 Outlook 帐户。
  • @arnt,我同意这不仅仅是一个编程问题,而是一个关于如何使用 Office365 (Exchange Online) 的 API 的问题。它是一个编程接口,因此也不仅仅是“询问 Microsoft 如何配置该 Outlook 帐户”的问题。让我们看看是否有人以前使用过这个 API 并了解它是如何工作的,或者可以为他指明正确的方向。您添加到“支持问题”的链接甚至说:“我们在这里帮助您应对编写代码的挑战。如果该代码涉及某些公司的 API,那很好。我们非常乐意为您提供帮助。”
  • @roberth 仅供参考,我有,并且确实理解。

标签: c# azure oauth-2.0 office365 imap


【解决方案1】:

你可以关注Scoping application permissions to specific Exchange Online mailboxes

虽然此文档在 Microsoft Graph 下,但它也应该适用于 https://outlook.office365.com 模块,因为此设置用于应用注册和 O365 邮箱。

您需要创建一个设置-AccessRight RestrictAccess 的应用程序访问策略。

然后测试新创建的限制访问用户user1@contoso.com的应用程序访问策略。

Test-ApplicationAccessPolicy -Identity user1@contoso.com -AppId e7e4dbfc-046-4074-9b3b-2ae8f144f59b

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 2017-03-21
    • 2018-06-09
    • 1970-01-01
    相关资源
    最近更新 更多