2
votes

I'm working on a C# application that would access current user's mailbox in office 365, but cannot go past authentication phase. I was using this example https://msdn.microsoft.com/en-us/library/office/dn567668(v=exchg.150).aspx

    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
    service.UseDefaultCredentials = false;
    //Specify login which works when connecting to Office 365 web UI
    service.Credentials = new WebCredentials("office_365_login@domain", "password");

    //would fail here with 401 unauthorised
    service.AutodiscoverUrl("email_address", RedirectionUrlValidationCallback);

    //As an alternative to autodiscover, I've tried to specify endpoint explicitly
    service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx")

    EmailMessage email = new EmailMessage(service);
    ...//Set email info
    //Would fail with 401 unauthorised
    email.Send();

Such code works just fine when connecting local Exchange mailboxes, but doesn't work with office 365 for some reason. I've tried using solution provided here, same result: Connection to Office 365 by EWS API

Infrastructure description: Our company uses hybrid Exchange setup: some mailboxes are located on local Exchange servers, some have been migrated to Office 365. We have also Microsoft ADFS server installed, used for office 365 authentication. When I open OWA URL in browser, I'm being first redirected to our ADFS server, if I'm using domain account, I'm being transparentry redirected to https://outlook.office.com/owa/ otherwise I'm being asked for credentials on our ADFS server.

Question: Is it possible to connect Office 365 mailbox by specifying credentials explicitly, like in the code example? Or should I get token from our ADFS server and use it to connect to Office 365? I haven't found C# examples on the latter one.

Goals, I'm trying to achieve:

  1. Perfect scenario. If user runs the app using domain account, application should be able to access his mailbox without specifying credentials explicitly
  2. Acceptable scenario. User has to set credentials explicitly in order for application to access mailbox
1

1 Answers

0
votes

In case you are trying to use EWS API for connecting to Office 365 mailbox, have a look to this:

You cannot use basic authentication (username and password) in your EWS application to connect to Office/Microsoft 365 now. Microsoft no longer supports basic authentication in EWS for Exchange Online. You are advised to use OAuth2.0 to get a token and use the same in your EWS client to connect to Office 365.

For this you will have to register your application in Azure AD to use client credential flow. I did a POC on this using console application. enter image description here

enter image description here

Here's the link to GitHub repository for source code and documentation.