如何在.NET应用程序中使用Microsoft Graph访问共享邮件文件夹

时间:2019-01-17 07:21:46

标签: asp.net-mvc microsoft-graph microsoft-graph-sdks

我已经从Microsoft Graph网站下载了ASP.NET MVC平台中的Microsoft Graph应用程序。我需要访问共享邮件文件夹,不确定该如何获取?在以下代码中,我可以访问我的mailFolder,但不能访问共享的Mailfolder!

public static async Task<IEnumerable<MailFolder>> GetMailFolderAsync()
{
    var graphClient = GetAuthenticatedClient();
    var mailFolder = await graphClient.Me.MailFolders.Request().GetAsync();
    var sharedMailFolder = await graphClient.Users.Request().GetAsync();
    return mailFolder;
}

此外,我想在上面的代码中知道我可以在哪里传递参数以访问下一页或所有页面?

private static GraphServiceClient GetAuthenticatedClient()
    {
        return new GraphServiceClient(
            new DelegateAuthenticationProvider(
                async (requestMessage) =>
                {
                    string signedInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
                    SessionTokenStore tokenStore = new SessionTokenStore(signedInUserId,
                        new HttpContextWrapper(HttpContext.Current));

                    var idClient = new ConfidentialClientApplication(
                        appId, redirectUri, new ClientCredential(appSecret),
                        tokenStore.GetMsalCacheInstance(), null);

                    var accounts = await idClient.GetAccountsAsync();

                    var result = await idClient.AcquireTokenSilentAsync(
                        graphScopes.Split(' '), accounts.FirstOrDefault());

                    requestMessage.Headers.Authorization =
                        new AuthenticationHeaderValue("Bearer", result.AccessToken);
                }));

1 个答案:

答案 0 :(得分:0)

我认为也无法访问正在调查的共享文件夹。关于获取页面的问题,只要您收到第一个请求

public static async Task<IEnumerable<MailFolder>> GetMailFolderAsync()
{
    var graphClient = GetAuthenticatedClient();
    var mailFolder = await graphClient.Me.MailFolders.Request().GetAsync();
    var sharedMailFolder = await graphClient.Users.Request().GetAsync();
    return mailFolder;
}

然后您可以查看例如mailFolder.NextPageRequest,如果它不为null,则可以通过执行mailFolder.NextPageRequest.GetAsync()来请求它,并将其用作有条件的循环

while(mailfoldersCollection != null) {
// Do your stuff with items within for(var folder in mailfoldersCollection) {}
// when read all items in CurrentPage then
if (mailFolder.NextPageRequest != null) {
mailfoldersCollection = await mailFolder.NextPageRequest.GetAsync();
}

希望它对您有用!