Sooraj Nair
Sooraj Nair

Reputation: 119

Is there a way to connect Azure AD MFA enabled outlook mailbox through a console application C#?

I have a console application written in c# which we use to connect to outlook mailbox using exchange service to read the emails from the inbox. Recently, we have migrated to Azure AD with MFA and after that change, my code is throwing 401 Unauthorized error. Can anyone suggest a workaround to authenticate the console to connect and read mail without any user dependency.

Normal WebClient function which uses username and password to connect to outlook. https://outlook.office365.com/EWS/Exchange.asmx is used as the Service object url in code.

Upvotes: 2

Views: 1719

Answers (1)

Marc
Marc

Reputation: 1041

Use ADAL.NET or MSAL.NET and OAuth2 authn to EWS: https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth That will handle whatever MFA you have set up. (I am assuming your application is running with the user present and thus able to authenticate. If this is an un-attended app running with user credentials hard-coded than you will either have to allow that user not to use MFA or change the app to login with a service principal, still using OAuth2 as above).

Upvotes: 2

Related Questions