Home > Software engineering >  What's the best way of web api authentication to Microsoft Graph to access emails in the servic
What's the best way of web api authentication to Microsoft Graph to access emails in the servic

Time:07-27

I am trying to figure out which way would be better for my application. I need to perform an automatic email import triggered from ASP.NET Core Web API using Microsoft Graph mailbox access. According to the documentation, there are two options to go for:

  1. Get access on behalf of the user (enter image description here

    Then pls note that you have to using client credential flow to generate access token to call the graph api, you can't use other flows. Here's sample code for using client credential flow with graph SDK to call graph api.

    var scopes = new[] { "https://graph.microsoft.com/.default" };
    var tenantId = "tenant_name.onmicrosoft.com";
    var clientId = "azure_ad_appid";
    var clientSecret = "client_secret";
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret);
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    var inboxMessages = await graphClient.Users["user_id"]
                            .MailFolders["inbox"]
                            .Messages.Request().GetAsync();
    
  • Related