C# Exchange Web Services Managed API Impersonation -> Microsoft Graph API

问题描述 投票:0回答:1

我有一个c#应用程序,可以查询我们的Microsoft Exchange服务器(现在的Exchange Online)。 它是使用 Microsoft.Exchange.WebServices .NET 库编写的。 IIS 中的应用程序池在 Exchange 中具有高权限的帐户下运行。 这使得它可以查询所有用户的日历,以便应用程序可以显示他们是否忙于办公或在其他地方工作。 _service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, emailAddress); 设置服务告诉服务,应用程序池账户将冒充用户(电子邮件地址)来查询日历。

说了这么多,Microsoft Exchange Web Services Managed API将在今年年底折旧。 我想用Microsoft Graph重写这个过程。 我找到了大量关于如何访问Exchange数据和查询日历的信息,通过使用 这个.

有没有人有什么好的例子,他们已经找到如何使用Microsoft Graph API来完成下面的功能? 是否有一个.NET包装器类我可以使用,或者我需要使用REST网络服务端点并创建我自己的?

public FindItemsResults<Appointment> GetCalendarAppointments(string emailAddress, string calendarName, DateTime start, DateTime end)
{
        // start with on prem exchange
        _service.UseDefaultCredentials = true; // use app pool security context
        _service.Url = new Uri(ConfigurationManager.ConnectionStrings["ExchangeURL"].ConnectionString);

        _service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, emailAddress);

        FolderView folderView = new FolderView(25);
        folderView.PropertySet = new PropertySet(BasePropertySet.IdOnly);
        folderView.PropertySet.Add(FolderSchema.DisplayName);
        SearchFilter searchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, calendarName);
        folderView.Traversal = FolderTraversal.Deep;
        FindFoldersResults findFolderResults = _service.FindFolders(WellKnownFolderName.Root, searchFilter, folderView);

        if (findFolderResults.TotalCount == 0)
            return null;

        FindItemsResults<Appointment> appointments;
        CalendarFolder calendarFolder;
        CalendarView calendarView = new CalendarView(start, end, 30);

        calendarView.PropertySet = new PropertySet(AppointmentSchema.Id,
                                                    AppointmentSchema.Start,
                                                    AppointmentSchema.End,
                                                    AppointmentSchema.Subject,
                                                    AppointmentSchema.Location);

        calendarFolder = (CalendarFolder)findFolderResults.Folders[0];

        try
        {
            appointments = calendarFolder.FindAppointments(calendarView);
        }
        catch (Exception e)
        {
            if (e.Message == "The SMTP address has no mailbox associated with it.")
            {
                // try exchange online
                _service.Credentials = new WebCredentials(ConfigurationManager.ConnectionStrings["ExchangeOnlineServiceAccountUsername"].ConnectionString,
                                                          ConfigurationManager.ConnectionStrings["ExchangeOnlineServiceAccountPassword"].ConnectionString);

                _service.Url = new Uri(ConfigurationManager.ConnectionStrings["ExchangeOnlineUrl"].ConnectionString);

                try
                {
                    appointments = calendarFolder.FindAppointments(calendarView);
                }
                catch (Exception ex)
                {
                    throw new Exception("Error when trying to read exchange to get calendar " + calendarName + " from exchange online inbox " + emailAddress + ": " + ex.Message);
                }

            }
            else
            {
                throw new Exception("Error when trying to read exchange to get calendar " + calendarName + " from on prem exchange inbox " + emailAddress + ": " + e.Message);
            }
        }

        if (appointments == null || appointments.Items.Count < 1)
            return null;

        return appointments;
}
c# microsoft-graph exchangewebservices impersonation outlook-calendar
1个回答
0
投票

@Eric 你可以使用微软提供的sdk,通过Graph API Endpoints实现上述功能。各种平台的sdk的概述以及示例可以在以下网站找到 此处.

你也可以试试 图形浏览器 和他们的邮差集合,以便对API端点有一个了解。

Github链接到 MS-GRAPH-DOTNET-SDK.

© www.soinside.com 2019 - 2024. All rights reserved.