How to get messages of user's mailbox from Exchange on-prem use modern authentication?

96 Views Asked by At

I have Exchange Server 2019 with enabled modern authentication. I also set up ADFS server. So when I access Outlook mailbox by the path https://example.com/oaw -> It redirects to ADFS server -> Enter credentials -> It goes to Outlook and I can see the messages on inbox.

So I implement code:

  • Get access token from ADFS server (completed).
  • Try to authenticate Exchange on-prem with the above access token using EWS Managed API but unsuccessful. The code is like this:
    var accessToken = GetAccessTokenAsync().Result;
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
    service.Url = new Uri("https://example.com/ews/exchange.asmx");
    service.Credentials = new OAuthCredentials(accessToken);
    service.AutodiscoverUrl("[email protected]", RedirectionCallback);
    FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, new ItemView(int.MaxValue));

But it got the error 'The request failed. The remote server returned an error: (401) Unauthorized.' It seems that the OAuthCredentials(accessToken) don't work.

Do you have any solutions or ideas to get the messages from the user mailbox from Exchange on-prem by modern authentication? Thank you.

0

There are 0 best solutions below